blob: edb537c449a2f0a84cee26cfe7775248bf592106 [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 Prijono44e88ea2007-10-30 15:42:35 +000030PJ_DEF(struct pjsua_data*) pjsua_get_var(void)
31{
32 return &pjsua_var;
33}
34
35
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036/* Display error */
37PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
38 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000039{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000040 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000041
Benny Prijonoeebe9af2006-06-13 22:57:13 +000042 pj_strerror(status, errmsg, sizeof(errmsg));
43 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000044}
45
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000048{
49 unsigned i;
50
Benny Prijonoc97608e2007-03-23 16:34:20 +000051 pj_bzero(&pjsua_var, sizeof(pjsua_var));
52
Benny Prijonoeebe9af2006-06-13 22:57:13 +000053 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
54 pjsua_var.acc[i].index = i;
55
56 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
57 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000058
59 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijono6ba8c542007-10-16 01:34:14 +000060 pjsua_var.nat_status = PJ_EPENDING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000061}
Benny Prijonodc39fe82006-05-26 12:17:46 +000062
63
Benny Prijono1f61a8f2007-08-16 10:11:44 +000064PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
65{
66 pj_bzero(cfg, sizeof(*cfg));
67
68 cfg->msg_logging = PJ_TRUE;
69 cfg->level = 5;
70 cfg->console_level = 4;
71 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
72 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
73}
74
75PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
76 pjsua_logging_config *dst,
77 const pjsua_logging_config *src)
78{
79 pj_memcpy(dst, src, sizeof(*src));
80 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
81}
82
83PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
84{
85 pj_bzero(cfg, sizeof(*cfg));
86
87 cfg->max_calls = 4;
88 cfg->thread_cnt = 1;
Benny Prijono91a6a172007-10-31 08:59:29 +000089 cfg->nat_type_in_sdp = 1;
Benny Prijonod8179652008-01-23 20:39:07 +000090#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
91 cfg->use_srtp = PJSUA_DEFAULT_USE_SRTP;
92 cfg->srtp_secure_signaling = PJSUA_DEFAULT_SRTP_SECURE_SIGNALING;
93#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +000094}
95
Benny Prijono1f61a8f2007-08-16 10:11:44 +000096PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
97 pjsua_config *dst,
98 const pjsua_config *src)
99{
100 unsigned i;
101
102 pj_memcpy(dst, src, sizeof(*src));
103
104 for (i=0; i<src->outbound_proxy_cnt; ++i) {
105 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
106 &src->outbound_proxy[i]);
107 }
108
109 for (i=0; i<src->cred_count; ++i) {
110 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
111 }
112
113 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
114 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
115 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
116 pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host);
117}
118
119PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
120{
121 pj_bzero(msg_data, sizeof(*msg_data));
122 pj_list_init(&msg_data->hdr_list);
123}
124
125PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
126{
127 pj_bzero(cfg, sizeof(*cfg));
128 pjsip_tls_setting_default(&cfg->tls_setting);
129}
130
131PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
132 pjsua_transport_config *dst,
133 const pjsua_transport_config *src)
134{
135 PJ_UNUSED_ARG(pool);
136 pj_memcpy(dst, src, sizeof(*src));
137}
138
139PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
140{
141 pj_bzero(cfg, sizeof(*cfg));
142
143 cfg->reg_timeout = PJSUA_REG_INTERVAL;
144 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijono15b02302007-09-27 14:07:07 +0000145 cfg->auto_update_nat = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000146 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000147 cfg->ka_interval = 15;
148 cfg->ka_data = pj_str("\r\n");
Benny Prijonod8179652008-01-23 20:39:07 +0000149#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
150 cfg->use_srtp = pjsua_var.ua_cfg.use_srtp;
151 cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling;
152#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000153}
154
155PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
156{
157 pj_bzero(cfg, sizeof(*cfg));
158}
159
160PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
161{
162 pj_bzero(cfg, sizeof(*cfg));
163
164 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono37c710b2008-01-10 12:09:26 +0000165 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
166 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000167 cfg->has_ioqueue = PJ_TRUE;
168 cfg->thread_cnt = 1;
169 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
170 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
171 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
172 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
173}
174
Benny Prijonodc39fe82006-05-26 12:17:46 +0000175
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000176/*****************************************************************************
177 * This is a very simple PJSIP module, whose sole purpose is to display
178 * incoming and outgoing messages to log. This module will have priority
179 * higher than transport layer, which means:
180 *
181 * - incoming messages will come to this module first before reaching
182 * transaction layer.
183 *
184 * - outgoing messages will come to this module last, after the message
185 * has been 'printed' to contiguous buffer by transport layer and
186 * appropriate transport instance has been decided for this message.
187 *
188 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000189
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000190/* Notification on incoming messages */
191static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
192{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000193 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
194 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000195 "--end msg--",
196 rdata->msg_info.len,
197 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000198 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 rdata->pkt_info.src_name,
200 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000201 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000202 rdata->msg_info.msg_buf));
203
204 /* Always return false, otherwise messages will not get processed! */
205 return PJ_FALSE;
206}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000207
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000208/* Notification on outgoing messages */
209static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
210{
211
212 /* Important note:
213 * tp_info field is only valid after outgoing messages has passed
214 * transport layer. So don't try to access tp_info when the module
215 * has lower priority than transport layer.
216 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000217
Benny Prijonob4a17c92006-07-10 14:40:21 +0000218 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
219 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000220 "--end msg--",
221 (tdata->buf.cur - tdata->buf.start),
222 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000223 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000224 tdata->tp_info.dst_name,
225 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000226 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000227 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000228
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000230 return PJ_SUCCESS;
231}
232
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233/* The module instance. */
234static pjsip_module pjsua_msg_logger =
235{
236 NULL, NULL, /* prev, next. */
237 { "mod-pjsua-log", 13 }, /* Name. */
238 -1, /* Id */
239 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
240 NULL, /* load() */
241 NULL, /* start() */
242 NULL, /* stop() */
243 NULL, /* unload() */
244 &logging_on_rx_msg, /* on_rx_request() */
245 &logging_on_rx_msg, /* on_rx_response() */
246 &logging_on_tx_msg, /* on_tx_request. */
247 &logging_on_tx_msg, /* on_tx_response() */
248 NULL, /* on_tsx_state() */
249
250};
251
252
253/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000254 * Another simple module to handle incoming OPTIONS request
255 */
256
257/* Notification on incoming request */
258static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
259{
260 pjsip_tx_data *tdata;
261 pjsip_response_addr res_addr;
Benny Prijono617c5bc2007-04-02 19:51:21 +0000262 pjmedia_sock_info skinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000263 pjmedia_sdp_session *sdp;
264 const pjsip_hdr *cap_hdr;
265 pj_status_t status;
266
267 /* Only want to handle OPTIONS requests */
268 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000269 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000270 {
271 return PJ_FALSE;
272 }
273
274 /* Create basic response. */
275 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
276 &tdata);
277 if (status != PJ_SUCCESS) {
278 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
279 return PJ_TRUE;
280 }
281
282 /* Add Allow header */
283 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
284 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000285 pjsip_msg_add_hdr(tdata->msg,
286 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000287 }
288
289 /* Add Accept header */
290 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
291 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000292 pjsip_msg_add_hdr(tdata->msg,
293 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000294 }
295
296 /* Add Supported header */
297 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
298 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000299 pjsip_msg_add_hdr(tdata->msg,
300 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000301 }
302
303 /* Add Allow-Events header from the evsub module */
304 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
305 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000306 pjsip_msg_add_hdr(tdata->msg,
307 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000308 }
309
310 /* Add User-Agent header */
311 if (pjsua_var.ua_cfg.user_agent.slen) {
312 const pj_str_t USER_AGENT = { "User-Agent", 10};
313 pjsip_hdr *h;
314
315 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
316 &USER_AGENT,
317 &pjsua_var.ua_cfg.user_agent);
318 pjsip_msg_add_hdr(tdata->msg, h);
319 }
320
Benny Prijono617c5bc2007-04-02 19:51:21 +0000321 /* Get media socket info */
322 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &skinfo);
323
Benny Prijono56315612006-07-18 14:39:40 +0000324 /* Add SDP body, using call0's RTP address */
325 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijono617c5bc2007-04-02 19:51:21 +0000326 &skinfo, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000327 if (status == PJ_SUCCESS) {
328 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
329 }
330
331 /* Send response statelessly */
332 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
333 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
334 if (status != PJ_SUCCESS)
335 pjsip_tx_data_dec_ref(tdata);
336
337 return PJ_TRUE;
338}
339
340
341/* The module instance. */
342static pjsip_module pjsua_options_handler =
343{
344 NULL, NULL, /* prev, next. */
345 { "mod-pjsua-options", 17 }, /* Name. */
346 -1, /* Id */
347 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
348 NULL, /* load() */
349 NULL, /* start() */
350 NULL, /* stop() */
351 NULL, /* unload() */
352 &options_on_rx_request, /* on_rx_request() */
353 NULL, /* on_rx_response() */
354 NULL, /* on_tx_request. */
355 NULL, /* on_tx_response() */
356 NULL, /* on_tsx_state() */
357
358};
359
360
361/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000362 * These two functions are the main callbacks registered to PJSIP stack
363 * to receive SIP request and response messages that are outside any
364 * dialogs and any transactions.
365 */
Benny Prijono268ca612006-02-07 12:34:11 +0000366
367/*
368 * Handler for receiving incoming requests.
369 *
370 * This handler serves multiple purposes:
371 * - it receives requests outside dialogs.
372 * - it receives requests inside dialogs, when the requests are
373 * unhandled by other dialog usages. Example of these
374 * requests are: MESSAGE.
375 */
376static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
377{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000378 pj_bool_t processed = PJ_FALSE;
379
380 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000381
Benny Prijono84126ab2006-02-09 09:30:09 +0000382 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000383
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000385 }
386
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 PJSUA_UNLOCK();
388
389 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000390}
391
392
393/*
394 * Handler for receiving incoming responses.
395 *
396 * This handler serves multiple purposes:
397 * - it receives strayed responses (i.e. outside any dialog and
398 * outside any transactions).
399 * - it receives responses coming to a transaction, when pjsua
400 * module is set as transaction user for the transaction.
401 * - it receives responses inside a dialog, when these responses
402 * are unhandled by other dialog usages.
403 */
404static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
405{
406 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000407 return PJ_FALSE;
408}
409
410
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000411/*****************************************************************************
412 * Logging.
413 */
414
415/* Log callback */
416static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000417{
Benny Prijono572d4852006-11-23 21:50:02 +0000418 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000419
420 if (pjsua_var.log_file) {
421 pj_ssize_t size = len;
422 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000423 /* This will slow things down considerably! Don't do it!
424 pj_file_flush(pjsua_var.log_file);
425 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000426 }
427
Benny Prijono572d4852006-11-23 21:50:02 +0000428 if (level <= (int)pjsua_var.log_cfg.console_level) {
429 if (pjsua_var.log_cfg.cb)
430 (*pjsua_var.log_cfg.cb)(level, buffer, len);
431 else
432 pj_log_write(level, buffer, len);
433 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000434}
435
436
437/*
438 * Application can call this function at any time (after pjsua_create(), of
439 * course) to change logging settings.
440 */
441PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
442{
443 pj_status_t status;
444
445 /* Save config. */
446 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
447
448 /* Redirect log function to ours */
449 pj_log_set_log_func( &log_writer );
450
Benny Prijono9cb09a22007-08-30 09:35:10 +0000451 /* Set decor */
452 pj_log_set_decor(pjsua_var.log_cfg.decor);
453
Benny Prijono4190cf92008-01-18 13:25:05 +0000454 /* Set log level */
455 pj_log_set_level(pjsua_var.log_cfg.level);
456
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000457 /* Close existing file, if any */
458 if (pjsua_var.log_file) {
459 pj_file_close(pjsua_var.log_file);
460 pjsua_var.log_file = NULL;
461 }
462
463 /* If output log file is desired, create the file: */
464 if (pjsua_var.log_cfg.log_filename.slen) {
465
466 status = pj_file_open(pjsua_var.pool,
467 pjsua_var.log_cfg.log_filename.ptr,
468 PJ_O_WRONLY,
469 &pjsua_var.log_file);
470
471 if (status != PJ_SUCCESS) {
472 pjsua_perror(THIS_FILE, "Error creating log file", status);
473 return status;
474 }
475 }
476
477 /* Unregister msg logging if it's previously registered */
478 if (pjsua_msg_logger.id >= 0) {
479 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
480 pjsua_msg_logger.id = -1;
481 }
482
483 /* Enable SIP message logging */
484 if (pjsua_var.log_cfg.msg_logging)
485 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
486
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000487 return PJ_SUCCESS;
488}
489
490
491/*****************************************************************************
492 * PJSUA Base API.
493 */
494
495/* Worker thread function. */
496static int worker_thread(void *arg)
497{
498 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000499
Benny Prijono268ca612006-02-07 12:34:11 +0000500 PJ_UNUSED_ARG(arg);
501
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 while (!pjsua_var.thread_quit_flag) {
503 int count;
504
505 count = pjsua_handle_events(TIMEOUT);
506 if (count < 0)
507 pj_thread_sleep(TIMEOUT);
508 }
Benny Prijono268ca612006-02-07 12:34:11 +0000509
510 return 0;
511}
512
Benny Prijonodc39fe82006-05-26 12:17:46 +0000513
Benny Prijono268ca612006-02-07 12:34:11 +0000514/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000515 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000516 */
517PJ_DEF(pj_status_t) pjsua_create(void)
518{
519 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000520
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000521 /* Init pjsua data */
522 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000523
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 /* Set default logging settings */
525 pjsua_logging_config_default(&pjsua_var.log_cfg);
526
527 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000528 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
530
Benny Prijono268ca612006-02-07 12:34:11 +0000531
Benny Prijonofccab712006-02-22 22:23:22 +0000532 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000533 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000534 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000535
Benny Prijonoec921342007-03-24 13:00:30 +0000536 /* Init PJNATH */
537 status = pjnath_init();
538 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000539
Benny Prijono094d3ad2006-11-21 08:41:00 +0000540 /* Set default sound device ID */
541 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
542
Benny Prijono268ca612006-02-07 12:34:11 +0000543 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000544 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000545
546 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
548
549 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000550
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000551 /* Create mutex */
552 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
553 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000554 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000556 return status;
557 }
558
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 /* Must create SIP endpoint to initialize SIP parser. The parser
560 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000561 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000562 status = pjsip_endpt_create(&pjsua_var.cp.factory,
563 pj_gethostname()->ptr,
564 &pjsua_var.endpt);
565 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000566
567
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000568 return PJ_SUCCESS;
569}
570
571
572/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000573 * Initialize pjsua with the specified settings. All the settings are
574 * optional, and the default values will be used when the config is not
575 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000576 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000577PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
578 const pjsua_logging_config *log_cfg,
579 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000580{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 pjsua_config default_cfg;
582 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000583 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000584 pj_status_t status;
585
586
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000587 /* Create default configurations when the config is not supplied */
588
589 if (ua_cfg == NULL) {
590 pjsua_config_default(&default_cfg);
591 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000592 }
593
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594 if (media_cfg == NULL) {
595 pjsua_media_config_default(&default_media_cfg);
596 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000597 }
598
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000599 /* Initialize logging first so that info/errors can be captured */
600 if (log_cfg) {
601 status = pjsua_reconfigure_logging(log_cfg);
602 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000603 }
604
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000605 /* If nameserver is configured, create DNS resolver instance and
606 * set it to be used by SIP resolver.
607 */
608 if (ua_cfg->nameserver_count) {
609#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000610 unsigned i;
611
612 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000613 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
614 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000615 if (status != PJ_SUCCESS) {
616 pjsua_perror(THIS_FILE, "Error creating resolver", status);
617 return status;
618 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000619
620 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000621 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
622 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000623 ua_cfg->nameserver, NULL);
624 if (status != PJ_SUCCESS) {
625 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
626 return status;
627 }
628
629 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000630 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000631 if (status != PJ_SUCCESS) {
632 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
633 return status;
634 }
635
636 /* Print nameservers */
637 for (i=0; i<ua_cfg->nameserver_count; ++i) {
638 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
639 (int)ua_cfg->nameserver[i].slen,
640 ua_cfg->nameserver[i].ptr));
641 }
642#else
643 PJ_LOG(2,(THIS_FILE,
644 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
645#endif
646 }
647
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000648 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000649
650 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000651 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
652 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000653
Benny Prijonodc39fe82006-05-26 12:17:46 +0000654
655 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000656 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
657 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000658
Benny Prijonodc39fe82006-05-26 12:17:46 +0000659
Benny Prijono053f5222006-11-11 16:16:04 +0000660 /* Initialize Replaces support. */
661 status = pjsip_replaces_init_module( pjsua_var.endpt );
662 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
663
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000664 /* Initialize 100rel support */
665 status = pjsip_100rel_init_module(pjsua_var.endpt);
666 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000667
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000668 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000669 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000670 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000671 {
672 NULL, NULL, /* prev, next. */
673 { "mod-pjsua", 9 }, /* Name. */
674 -1, /* Id */
675 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
676 NULL, /* load() */
677 NULL, /* start() */
678 NULL, /* stop() */
679 NULL, /* unload() */
680 &mod_pjsua_on_rx_request, /* on_rx_request() */
681 &mod_pjsua_on_rx_response, /* on_rx_response() */
682 NULL, /* on_tx_request. */
683 NULL, /* on_tx_response() */
684 NULL, /* on_tsx_state() */
685 };
686
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000687 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000688
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000689 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
690 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000691 }
692
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000693
Benny Prijonodc39fe82006-05-26 12:17:46 +0000694
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000695 /* Initialize PJSUA call subsystem: */
696 status = pjsua_call_subsys_init(ua_cfg);
697 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000698 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000699
700
Benny Prijonoc97608e2007-03-23 16:34:20 +0000701 /* Start resolving STUN server */
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000702
Benny Prijonoc97608e2007-03-23 16:34:20 +0000703 status = pjsua_resolve_stun_server(PJ_FALSE);
704 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
705 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
706 return status;
707 }
708
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000709 /* Initialize PJSUA media subsystem */
710 status = pjsua_media_subsys_init(media_cfg);
711 if (status != PJ_SUCCESS)
712 goto on_error;
713
Benny Prijonodc39fe82006-05-26 12:17:46 +0000714
715 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000716 status = pjsip_evsub_init_module(pjsua_var.endpt);
717 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000718
Benny Prijonodc39fe82006-05-26 12:17:46 +0000719
720 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
722 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000723
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000724 /* Init PUBLISH module */
725 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000726
727 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000728 status = pjsip_xfer_init_module( pjsua_var.endpt );
729 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000730
731 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000732 status = pjsua_pres_init();
733 if (status != PJ_SUCCESS)
734 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000735
736 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000737 status = pjsua_im_init();
738 if (status != PJ_SUCCESS)
739 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000740
Benny Prijonoc8141a82006-08-20 09:12:19 +0000741 /* Register OPTIONS handler */
742 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
743
744 /* Add OPTIONS in Allow header */
745 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
746 NULL, 1, &STR_OPTIONS);
747
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000748 /* Start worker thread if needed. */
749 if (pjsua_var.ua_cfg.thread_cnt) {
750 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000751
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000752 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
753 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000754
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
756 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
757 NULL, 0, 0, &pjsua_var.thread[i]);
758 if (status != PJ_SUCCESS)
759 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000760 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000761 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
762 pjsua_var.ua_cfg.thread_cnt));
763 } else {
764 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000765 }
766
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000767 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000768
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000769 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000770 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000771
Benny Prijono268ca612006-02-07 12:34:11 +0000772 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000773
774on_error:
775 pjsua_destroy();
776 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000777}
778
779
Benny Prijono834aee32006-02-19 01:38:06 +0000780/* Sleep with polling */
781static void busy_sleep(unsigned msec)
782{
Benny Prijonob2c96822007-05-03 13:31:21 +0000783#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Benny Prijono897f9f82007-05-03 19:56:21 +0000784 /* Ideally we shouldn't call pj_thread_sleep() and rather
785 * CActiveScheduler::WaitForAnyRequest() here, but that will
786 * drag in Symbian header and it doesn't look pretty.
787 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000788 pj_thread_sleep(msec);
789#else
Benny Prijono834aee32006-02-19 01:38:06 +0000790 pj_time_val timeout, now;
791
792 pj_gettimeofday(&timeout);
793 timeout.msec += msec;
794 pj_time_val_normalize(&timeout);
795
796 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000797 while (pjsua_handle_events(10) > 0)
798 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000799 pj_gettimeofday(&now);
800 } while (PJ_TIME_VAL_LT(now, timeout));
Benny Prijonob2c96822007-05-03 13:31:21 +0000801#endif
Benny Prijono834aee32006-02-19 01:38:06 +0000802}
803
Benny Prijonoebbf6892007-03-24 17:37:25 +0000804
Benny Prijonoebbf6892007-03-24 17:37:25 +0000805/*
806 * Callback function to receive notification from the resolver
807 * when the resolution process completes.
808 */
809static void stun_dns_srv_resolver_cb(void *user_data,
810 pj_status_t status,
811 const pj_dns_srv_record *rec)
812{
813 unsigned i;
814
815 PJ_UNUSED_ARG(user_data);
816
817 pjsua_var.stun_status = status;
818
819 if (status != PJ_SUCCESS) {
820 /* DNS SRV resolution failed. If stun_host is specified, resolve
821 * it with gethostbyname()
822 */
823 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000824 pj_str_t str_host, str_port;
825 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000826 pj_hostent he;
827
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000828 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
829 if (str_port.ptr != NULL) {
830 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
831 str_host.slen = (str_port.ptr - str_host.ptr);
832 str_port.ptr++;
833 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
834 str_host.slen - 1;
835 port = (int)pj_strtoul(&str_port);
836 if (port < 1 || port > 65535) {
837 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
838 pjsua_var.stun_status = PJ_EINVAL;
839 return;
840 }
841 } else {
842 str_host = pjsua_var.ua_cfg.stun_host;
843 port = 3478;
844 }
845
846 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000847
848 if (pjsua_var.stun_status == PJ_SUCCESS) {
849 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
850 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000851 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000852
853 PJ_LOG(3,(THIS_FILE,
854 "STUN server %.*s resolved, address is %s:%d",
855 (int)pjsua_var.ua_cfg.stun_host.slen,
856 pjsua_var.ua_cfg.stun_host.ptr,
857 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
858 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
859 }
860 } else {
861 char errmsg[PJ_ERR_MSG_SIZE];
862
863 pj_strerror(status, errmsg, sizeof(errmsg));
864 PJ_LOG(1,(THIS_FILE,
865 "DNS SRV resolution failed for STUN server %.*s: %s",
866 (int)pjsua_var.ua_cfg.stun_domain.slen,
867 pjsua_var.ua_cfg.stun_domain.ptr,
868 errmsg));
869 }
870 return;
871 }
872
Benny Prijonof5afa922007-06-11 16:51:18 +0000873 pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0);
874 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL,
875 rec->entry[0].port);
876 pjsua_var.stun_srv.ipv4.sin_addr.s_addr =
877 rec->entry[0].server.addr[0].s_addr;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000878
879 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
880 (int)pjsua_var.ua_cfg.stun_domain.slen,
881 pjsua_var.ua_cfg.stun_domain.ptr,
882 rec->count));
883
884 for (i=0; i<rec->count; ++i) {
885 PJ_LOG(3,(THIS_FILE,
886 " %d: prio=%d, weight=%d %s:%d",
887 i, rec->entry[i].priority, rec->entry[i].weight,
Benny Prijonof5afa922007-06-11 16:51:18 +0000888 pj_inet_ntoa(rec->entry[i].server.addr[0]),
889 (int)rec->entry[i].port));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000890 }
891
892}
893
Benny Prijono268ca612006-02-07 12:34:11 +0000894/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000895 * Resolve STUN server.
896 */
897pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
898{
899 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
900 /* Initialize STUN configuration */
901 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
902 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
903 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
904
905 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000906
907 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000908
Benny Prijonoebbf6892007-03-24 17:37:25 +0000909 /* If stun_domain is specified, resolve STUN servers with DNS
910 * SRV resolution.
911 */
912 if (pjsua_var.ua_cfg.stun_domain.slen) {
913 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000914 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000915
916 /* Fail if resolver is not configured */
917 if (pjsua_var.resolver == NULL) {
918 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
919 "stun_domain is specified"));
920 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
921 return PJLIB_UTIL_EDNSNONS;
922 }
923 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000924 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000925 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
926 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonof5afa922007-06-11 16:51:18 +0000927 0, NULL, &stun_dns_srv_resolver_cb, NULL);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000928 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000929 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
930 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000931 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000932 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000933 } else {
934 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000935 }
936 }
937 /* Otherwise if stun_host is specified, resolve STUN server with
938 * gethostbyname().
939 */
940 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000941 pj_str_t str_host, str_port;
942 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000943 pj_hostent he;
944
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000945 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
946 if (str_port.ptr != NULL) {
947 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
948 str_host.slen = (str_port.ptr - str_host.ptr);
949 str_port.ptr++;
950 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
951 str_host.slen - 1;
952 port = (int)pj_strtoul(&str_port);
953 if (port < 1 || port > 65535) {
954 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
955 pjsua_var.stun_status = PJ_EINVAL;
956 return pjsua_var.stun_status;
957 }
958 } else {
959 str_host = pjsua_var.ua_cfg.stun_host;
960 port = 3478;
961 }
962
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000963 pjsua_var.stun_status =
964 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, &str_host,
965 (pj_uint16_t)port);
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000966
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000967 if (pjsua_var.stun_status != PJ_SUCCESS) {
968 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000969
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000970 if (pjsua_var.stun_status == PJ_SUCCESS) {
971 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
972 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
973 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
974 }
Benny Prijonoc97608e2007-03-23 16:34:20 +0000975 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000976
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000977 PJ_LOG(3,(THIS_FILE,
978 "STUN server %.*s resolved, address is %s:%d",
979 (int)pjsua_var.ua_cfg.stun_host.slen,
980 pjsua_var.ua_cfg.stun_host.ptr,
981 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
982 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
983
Benny Prijonoc97608e2007-03-23 16:34:20 +0000984 }
Benny Prijonoebbf6892007-03-24 17:37:25 +0000985 /* Otherwise disable STUN. */
986 else {
987 pjsua_var.stun_status = PJ_SUCCESS;
988 }
989
990
Benny Prijonoc97608e2007-03-23 16:34:20 +0000991 return pjsua_var.stun_status;
992
993 } else if (pjsua_var.stun_status == PJ_EPENDING) {
994 /* STUN server resolution has been started, wait for the
995 * result.
996 */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000997 if (wait) {
998 while (pjsua_var.stun_status == PJ_EPENDING)
999 pjsua_handle_events(10);
1000 }
1001
1002 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001003
1004 } else {
1005 /* STUN server has been resolved, return the status */
1006 return pjsua_var.stun_status;
1007 }
1008}
1009
1010/*
Benny Prijono268ca612006-02-07 12:34:11 +00001011 * Destroy pjsua.
1012 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001013PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001014{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001015 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001016
1017 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001018 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001019
1020 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001021 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1022 if (pjsua_var.thread[i]) {
1023 pj_thread_join(pjsua_var.thread[i]);
1024 pj_thread_destroy(pjsua_var.thread[i]);
1025 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001026 }
Benny Prijono268ca612006-02-07 12:34:11 +00001027 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001028
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001029 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001030 /* Terminate all calls. */
1031 pjsua_call_hangup_all();
1032
1033 /* Terminate all presence subscriptions. */
1034 pjsua_pres_shutdown();
1035
1036 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001037 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1038 if (!pjsua_var.acc[i].valid)
1039 continue;
1040
1041 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001042 pjsua_acc_set_registration(i, PJ_FALSE);
1043 }
1044 }
1045
1046 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001047 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1048 busy_sleep(1000);
1049 }
Benny Prijono834aee32006-02-19 01:38:06 +00001050
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001051 /* Destroy media */
1052 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001053
Benny Prijono268ca612006-02-07 12:34:11 +00001054 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 if (pjsua_var.endpt) {
1056 pjsip_endpt_destroy(pjsua_var.endpt);
1057 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001058 }
Benny Prijono268ca612006-02-07 12:34:11 +00001059
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001060 /* Destroy mutex */
1061 if (pjsua_var.mutex) {
1062 pj_mutex_destroy(pjsua_var.mutex);
1063 pjsua_var.mutex = NULL;
1064 }
1065
1066 /* Destroy pool and pool factory. */
1067 if (pjsua_var.pool) {
1068 pj_pool_release(pjsua_var.pool);
1069 pjsua_var.pool = NULL;
1070 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001071
1072 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1073
1074 /* End logging */
1075 if (pjsua_var.log_file) {
1076 pj_file_close(pjsua_var.log_file);
1077 pjsua_var.log_file = NULL;
1078 }
1079
1080 /* Shutdown PJLIB */
1081 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001082 }
Benny Prijono268ca612006-02-07 12:34:11 +00001083
Benny Prijonof762ee72006-12-01 11:14:37 +00001084 /* Clear pjsua_var */
1085 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1086
Benny Prijono268ca612006-02-07 12:34:11 +00001087 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001088 return PJ_SUCCESS;
1089}
1090
1091
1092/**
1093 * Application is recommended to call this function after all initialization
1094 * is done, so that the library can do additional checking set up
1095 * additional
1096 *
1097 * @return PJ_SUCCESS on success, or the appropriate error code.
1098 */
1099PJ_DEF(pj_status_t) pjsua_start(void)
1100{
1101 pj_status_t status;
1102
1103 status = pjsua_call_subsys_start();
1104 if (status != PJ_SUCCESS)
1105 return status;
1106
1107 status = pjsua_media_subsys_start();
1108 if (status != PJ_SUCCESS)
1109 return status;
1110
1111 status = pjsua_pres_start();
1112 if (status != PJ_SUCCESS)
1113 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001114
1115 return PJ_SUCCESS;
1116}
1117
Benny Prijono9fc735d2006-05-28 14:58:12 +00001118
1119/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001120 * Poll pjsua for events, and if necessary block the caller thread for
1121 * the specified maximum interval (in miliseconds).
1122 */
1123PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1124{
Benny Prijono897f9f82007-05-03 19:56:21 +00001125#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
1126 /* Ideally we shouldn't call pj_thread_sleep() and rather
1127 * CActiveScheduler::WaitForAnyRequest() here, but that will
1128 * drag in Symbian header and it doesn't look pretty.
1129 */
1130 pj_thread_sleep(msec_timeout);
1131 return msec_timeout;
1132#else
1133
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001134 unsigned count = 0;
1135 pj_time_val tv;
1136 pj_status_t status;
1137
1138 tv.sec = 0;
1139 tv.msec = msec_timeout;
1140 pj_time_val_normalize(&tv);
1141
1142 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1143
1144 if (status != PJ_SUCCESS)
1145 return -status;
1146
1147 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001148#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001149}
1150
1151
1152/*
1153 * Create memory pool.
1154 */
1155PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1156 pj_size_t increment)
1157{
1158 /* Pool factory is thread safe, no need to lock */
1159 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1160 NULL);
1161}
1162
1163
1164/*
1165 * Internal function to get SIP endpoint instance of pjsua, which is
1166 * needed for example to register module, create transports, etc.
1167 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001168 */
1169PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1170{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001171 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001172}
1173
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001174/*
1175 * Internal function to get media endpoint instance.
1176 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001177 */
1178PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1179{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001180 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001181}
1182
Benny Prijono97b87172006-08-24 14:25:14 +00001183/*
1184 * Internal function to get PJSUA pool factory.
1185 */
1186PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1187{
1188 return &pjsua_var.cp.factory;
1189}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001190
1191/*****************************************************************************
1192 * PJSUA SIP Transport API.
1193 */
1194
1195/*
Benny Prijono23674a32007-12-01 08:59:25 +00001196 * Tools to get address string.
1197 */
1198static const char *addr_string(const pj_sockaddr_t *addr)
1199{
1200 static char str[128];
1201 str[0] = '\0';
1202 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1203 pj_sockaddr_get_addr(addr),
1204 str, sizeof(str));
1205 return str;
1206}
1207
1208/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001209 * Create and initialize SIP socket (and possibly resolve public
1210 * address via STUN, depending on config).
1211 */
Benny Prijono23674a32007-12-01 08:59:25 +00001212static pj_status_t create_sip_udp_sock(int af,
1213 const pj_str_t *bind_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001214 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001215 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001216 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001217{
Benny Prijono23674a32007-12-01 08:59:25 +00001218 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijonoc97608e2007-03-23 16:34:20 +00001219 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001220 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001221 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001222 pj_status_t status;
1223
Benny Prijonoc97608e2007-03-23 16:34:20 +00001224 /* Make sure STUN server resolution has completed */
1225 status = pjsua_resolve_stun_server(PJ_TRUE);
1226 if (status != PJ_SUCCESS) {
1227 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1228 return status;
1229 }
1230
Benny Prijono23674a32007-12-01 08:59:25 +00001231 /* Initialize bound address */
1232 if (bind_param->slen) {
1233 status = pj_sockaddr_init(af, &bind_addr, bind_param,
1234 (pj_uint16_t)port);
1235 if (status != PJ_SUCCESS) {
1236 pjsua_perror(THIS_FILE,
1237 "Unable to resolve transport bound address",
1238 status);
1239 return status;
1240 }
1241 } else {
1242 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1243 }
1244
1245 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001246 if (status != PJ_SUCCESS) {
1247 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001248 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001249 }
1250
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001251 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001252 if (status != PJ_SUCCESS) {
1253 pjsua_perror(THIS_FILE, "bind() error", status);
1254 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001255 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001256 }
1257
Benny Prijonoe347cb02007-02-14 14:36:13 +00001258 /* If port is zero, get the bound port */
1259 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001260 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001261 int namelen = sizeof(bound_addr);
1262 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1263 if (status != PJ_SUCCESS) {
1264 pjsua_perror(THIS_FILE, "getsockname() error", status);
1265 pj_sock_close(sock);
1266 return status;
1267 }
1268
Benny Prijono23674a32007-12-01 08:59:25 +00001269 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001270 }
1271
Benny Prijonoc97608e2007-03-23 16:34:20 +00001272 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001273 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1274 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001275 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001276 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001277 }
1278
1279 /* Get the published address, either by STUN or by resolving
1280 * the name of local host.
1281 */
Benny Prijono23674a32007-12-01 08:59:25 +00001282 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001283 /*
1284 * Public address is already specified, no need to resolve the
1285 * address, only set the port.
1286 */
Benny Prijono23674a32007-12-01 08:59:25 +00001287 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1288 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001289
1290 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001291 /*
1292 * STUN is specified, resolve the address with STUN.
1293 */
Benny Prijono23674a32007-12-01 08:59:25 +00001294 if (af != pj_AF_INET()) {
1295 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1296 pj_sock_close(sock);
1297 return PJ_EAFNOTSUP;
1298 }
1299
Benny Prijono14c2b862007-02-21 00:40:05 +00001300 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001301 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1302 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001303 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001304 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001305 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001306 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001307 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001308 }
1309
1310 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001311 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001312
Benny Prijono42d08d22007-12-20 11:23:07 +00001313 if (pj_sockaddr_has_addr(&bind_addr)) {
1314 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1315 } else {
1316 status = pj_gethostip(af, p_pub_addr);
1317 if (status != PJ_SUCCESS) {
1318 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1319 pj_sock_close(sock);
1320 return status;
1321 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001322 }
1323
Benny Prijono23674a32007-12-01 08:59:25 +00001324 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1325 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001326 }
1327
1328 *p_sock = sock;
1329
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001331 addr_string(p_pub_addr),
1332 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001333
Benny Prijonod8410532006-06-15 11:04:33 +00001334 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001335}
1336
1337
1338/*
1339 * Create SIP transport.
1340 */
1341PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1342 const pjsua_transport_config *cfg,
1343 pjsua_transport_id *p_id)
1344{
1345 pjsip_transport *tp;
1346 unsigned id;
1347 pj_status_t status;
1348
1349 PJSUA_LOCK();
1350
1351 /* Find empty transport slot */
1352 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001353 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001354 break;
1355 }
1356
1357 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1358 status = PJ_ETOOMANY;
1359 pjsua_perror(THIS_FILE, "Error creating transport", status);
1360 goto on_return;
1361 }
1362
1363 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001364 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001365 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001366 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001367 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001368 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001369 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001370 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001371 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001372 pjsip_host_port addr_name;
1373
1374 /* Supply default config if it's not specified */
1375 if (cfg == NULL) {
1376 pjsua_transport_config_default(&config);
1377 cfg = &config;
1378 }
1379
Benny Prijono0a5cad82006-09-26 13:21:02 +00001380 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001381 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1382 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001383 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001384 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1385 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001386 if (status != PJ_SUCCESS) {
1387 pjsua_perror(THIS_FILE,
1388 "Unable to resolve transport public address",
1389 status);
1390 goto on_return;
1391 }
1392 }
1393
1394 /* Create the socket and possibly resolve the address with STUN
1395 * (only when public address is not specified).
1396 */
Benny Prijono23674a32007-12-01 08:59:25 +00001397 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
1398 &cfg->bound_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001399 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001400 if (status != PJ_SUCCESS)
1401 goto on_return;
1402
Benny Prijono23674a32007-12-01 08:59:25 +00001403 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1404 addr_name.host = pj_str(hostbuf);
1405 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001406
1407 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001408 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1409 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001410 if (status != PJ_SUCCESS) {
1411 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1412 status);
1413 pj_sock_close(sock);
1414 goto on_return;
1415 }
1416
Benny Prijonoe93e2872006-06-28 16:46:49 +00001417
1418 /* Save the transport */
1419 pjsua_var.tpdata[id].type = type;
1420 pjsua_var.tpdata[id].local_name = tp->local_name;
1421 pjsua_var.tpdata[id].data.tp = tp;
1422
Benny Prijono3569c0d2007-04-06 10:29:20 +00001423#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1424
Benny Prijonob2477142007-12-05 04:09:59 +00001425 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001426 /*
1427 * Create TCP transport.
1428 */
1429 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001430 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001431 pjsip_tpfactory *tcp;
1432 pj_sockaddr_in local_addr;
1433
1434 /* Supply default config if it's not specified */
1435 if (cfg == NULL) {
1436 pjsua_transport_config_default(&config);
1437 cfg = &config;
1438 }
1439
1440 /* Init local address */
1441 pj_sockaddr_in_init(&local_addr, 0, 0);
1442
1443 if (cfg->port)
1444 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1445
Benny Prijono0a5cad82006-09-26 13:21:02 +00001446 if (cfg->bound_addr.slen) {
1447 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1448 if (status != PJ_SUCCESS) {
1449 pjsua_perror(THIS_FILE,
1450 "Unable to resolve transport bound address",
1451 status);
1452 goto on_return;
1453 }
1454 }
1455
1456 /* Init published name */
1457 pj_bzero(&a_name, sizeof(pjsip_host_port));
1458 if (cfg->public_addr.slen)
1459 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001460
1461 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001462 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1463 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001464
1465 if (status != PJ_SUCCESS) {
1466 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1467 status);
1468 goto on_return;
1469 }
1470
1471 /* Save the transport */
1472 pjsua_var.tpdata[id].type = type;
1473 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1474 pjsua_var.tpdata[id].data.factory = tcp;
1475
Benny Prijono3569c0d2007-04-06 10:29:20 +00001476#endif /* PJ_HAS_TCP */
1477
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001478#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1479 } else if (type == PJSIP_TRANSPORT_TLS) {
1480 /*
1481 * Create TLS transport.
1482 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001483 /*
1484 * Create TCP transport.
1485 */
1486 pjsua_transport_config config;
1487 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001488 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001489 pj_sockaddr_in local_addr;
1490
1491 /* Supply default config if it's not specified */
1492 if (cfg == NULL) {
1493 pjsua_transport_config_default(&config);
1494 config.port = 5061;
1495 cfg = &config;
1496 }
1497
1498 /* Init local address */
1499 pj_sockaddr_in_init(&local_addr, 0, 0);
1500
1501 if (cfg->port)
1502 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1503
1504 if (cfg->bound_addr.slen) {
1505 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1506 if (status != PJ_SUCCESS) {
1507 pjsua_perror(THIS_FILE,
1508 "Unable to resolve transport bound address",
1509 status);
1510 goto on_return;
1511 }
1512 }
1513
1514 /* Init published name */
1515 pj_bzero(&a_name, sizeof(pjsip_host_port));
1516 if (cfg->public_addr.slen)
1517 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001518
1519 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001520 &cfg->tls_setting,
1521 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001522 if (status != PJ_SUCCESS) {
1523 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1524 status);
1525 goto on_return;
1526 }
1527
1528 /* Save the transport */
1529 pjsua_var.tpdata[id].type = type;
1530 pjsua_var.tpdata[id].local_name = tls->addr_name;
1531 pjsua_var.tpdata[id].data.factory = tls;
1532#endif
1533
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001534 } else {
1535 status = PJSIP_EUNSUPTRANSPORT;
1536 pjsua_perror(THIS_FILE, "Error creating transport", status);
1537 goto on_return;
1538 }
1539
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001540
1541 /* Return the ID */
1542 if (p_id) *p_id = id;
1543
1544 status = PJ_SUCCESS;
1545
1546on_return:
1547
1548 PJSUA_UNLOCK();
1549
Benny Prijonod8410532006-06-15 11:04:33 +00001550 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001551}
1552
1553
1554/*
1555 * Register transport that has been created by application.
1556 */
1557PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1558 pjsua_transport_id *p_id)
1559{
1560 unsigned id;
1561
1562 PJSUA_LOCK();
1563
1564 /* Find empty transport slot */
1565 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001566 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001567 break;
1568 }
1569
1570 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1571 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1572 PJSUA_UNLOCK();
1573 return PJ_ETOOMANY;
1574 }
1575
1576 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001577 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001578 pjsua_var.tpdata[id].local_name = tp->local_name;
1579 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001580
1581 /* Return the ID */
1582 if (p_id) *p_id = id;
1583
1584 PJSUA_UNLOCK();
1585
1586 return PJ_SUCCESS;
1587}
1588
1589
1590/*
1591 * Enumerate all transports currently created in the system.
1592 */
1593PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1594 unsigned *p_count )
1595{
1596 unsigned i, count;
1597
1598 PJSUA_LOCK();
1599
1600 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1601 ++i)
1602 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001603 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001604 continue;
1605
1606 id[count++] = i;
1607 }
1608
1609 *p_count = count;
1610
1611 PJSUA_UNLOCK();
1612
1613 return PJ_SUCCESS;
1614}
1615
1616
1617/*
1618 * Get information about transports.
1619 */
1620PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1621 pjsua_transport_info *info)
1622{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001623 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001624 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001625
Benny Prijonoac623b32006-07-03 15:19:31 +00001626 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001627
1628 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001629 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1630 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001631
1632 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001633 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001634
1635 PJSUA_LOCK();
1636
Benny Prijonoe93e2872006-06-28 16:46:49 +00001637 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1638
1639 pjsip_transport *tp = t->data.tp;
1640
1641 if (tp == NULL) {
1642 PJSUA_UNLOCK();
1643 return PJ_EINVALIDOP;
1644 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645
Benny Prijonoe93e2872006-06-28 16:46:49 +00001646 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001647 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001648 info->type_name = pj_str(tp->type_name);
1649 info->info = pj_str(tp->info);
1650 info->flag = tp->flag;
1651 info->addr_len = tp->addr_len;
1652 info->local_addr = tp->local_addr;
1653 info->local_name = tp->local_name;
1654 info->usage_count = pj_atomic_get(tp->ref_cnt);
1655
Benny Prijono0a5cad82006-09-26 13:21:02 +00001656 status = PJ_SUCCESS;
1657
Benny Prijonoe93e2872006-06-28 16:46:49 +00001658 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1659
1660 pjsip_tpfactory *factory = t->data.factory;
1661
1662 if (factory == NULL) {
1663 PJSUA_UNLOCK();
1664 return PJ_EINVALIDOP;
1665 }
1666
1667 info->id = id;
1668 info->type = t->type;
1669 info->type_name = pj_str("TCP");
1670 info->info = pj_str("TCP transport");
1671 info->flag = factory->flag;
1672 info->addr_len = sizeof(factory->local_addr);
1673 info->local_addr = factory->local_addr;
1674 info->local_name = factory->addr_name;
1675 info->usage_count = 0;
1676
Benny Prijono0a5cad82006-09-26 13:21:02 +00001677 status = PJ_SUCCESS;
1678
1679 } else {
1680 pj_assert(!"Unsupported transport");
1681 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001682 }
1683
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001684
1685 PJSUA_UNLOCK();
1686
Benny Prijono0a5cad82006-09-26 13:21:02 +00001687 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001688}
1689
1690
1691/*
1692 * Disable a transport or re-enable it.
1693 */
1694PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1695 pj_bool_t enabled)
1696{
1697 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001698 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1699 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001700
1701 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001702 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001703
1704
1705 /* To be done!! */
1706 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001707 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001708
1709 return PJ_EINVALIDOP;
1710}
1711
1712
1713/*
1714 * Close the transport.
1715 */
1716PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1717 pj_bool_t force )
1718{
Benny Prijono5ff61872007-02-01 03:37:11 +00001719 pj_status_t status;
1720
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001721 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001722 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1723 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724
1725 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001726 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001727
Benny Prijono0a5cad82006-09-26 13:21:02 +00001728 /* Note: destroy() may not work if there are objects still referencing
1729 * the transport.
1730 */
1731 if (force) {
1732 switch (pjsua_var.tpdata[id].type) {
1733 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001734 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1735 if (status != PJ_SUCCESS)
1736 return status;
1737 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1738 if (status != PJ_SUCCESS)
1739 return status;
1740 break;
1741
1742 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001743 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001744 /* This will close the TCP listener, but existing TCP/TLS
1745 * connections (if any) will still linger
1746 */
1747 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1748 (pjsua_var.tpdata[id].data.factory);
1749 if (status != PJ_SUCCESS)
1750 return status;
1751
Benny Prijono0a5cad82006-09-26 13:21:02 +00001752 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001753
Benny Prijono1ebd6142006-10-19 15:48:02 +00001754 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001755 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001756 }
1757
1758 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001759 /* If force is not specified, transports will be closed at their
1760 * convenient time. However this will leak PJSUA-API transport
1761 * descriptors as PJSUA-API wouldn't know when exactly the
1762 * transport is closed thus it can't cleanup PJSUA transport
1763 * descriptor.
1764 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001765 switch (pjsua_var.tpdata[id].type) {
1766 case PJSIP_TRANSPORT_UDP:
1767 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001768 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001769 case PJSIP_TRANSPORT_TCP:
1770 return (*pjsua_var.tpdata[id].data.factory->destroy)
1771 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001772 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001773 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001774 }
1775 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001776
Benny Prijono5ff61872007-02-01 03:37:11 +00001777 /* Cleanup pjsua data when force is applied */
1778 if (force) {
1779 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1780 pjsua_var.tpdata[id].data.ptr = NULL;
1781 }
1782
1783 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001784}
1785
1786
1787/*
1788 * Add additional headers etc in msg_data specified by application
1789 * when sending requests.
1790 */
1791void pjsua_process_msg_data(pjsip_tx_data *tdata,
1792 const pjsua_msg_data *msg_data)
1793{
1794 pj_bool_t allow_body;
1795 const pjsip_hdr *hdr;
1796
Benny Prijono56315612006-07-18 14:39:40 +00001797 /* Always add User-Agent */
1798 if (pjsua_var.ua_cfg.user_agent.slen &&
1799 tdata->msg->type == PJSIP_REQUEST_MSG)
1800 {
1801 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1802 pjsip_hdr *h;
1803 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1804 &STR_USER_AGENT,
1805 &pjsua_var.ua_cfg.user_agent);
1806 pjsip_msg_add_hdr(tdata->msg, h);
1807 }
1808
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001809 if (!msg_data)
1810 return;
1811
1812 hdr = msg_data->hdr_list.next;
1813 while (hdr && hdr != &msg_data->hdr_list) {
1814 pjsip_hdr *new_hdr;
1815
Benny Prijonoa1e69682007-05-11 15:14:34 +00001816 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001817 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1818
1819 hdr = hdr->next;
1820 }
1821
1822 allow_body = (tdata->msg->body == NULL);
1823
1824 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1825 pjsip_media_type ctype;
1826 pjsip_msg_body *body;
1827
1828 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1829 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1830 &msg_data->msg_body);
1831 tdata->msg->body = body;
1832 }
1833}
1834
1835
1836/*
1837 * Add route_set to outgoing requests
1838 */
1839void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1840 const pjsip_route_hdr *route_set )
1841{
1842 const pjsip_route_hdr *r;
1843
1844 r = route_set->next;
1845 while (r != route_set) {
1846 pjsip_route_hdr *new_r;
1847
Benny Prijonoa1e69682007-05-11 15:14:34 +00001848 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001849 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1850
1851 r = r->next;
1852 }
1853}
1854
1855
1856/*
1857 * Simple version of MIME type parsing (it doesn't support parameters)
1858 */
1859void pjsua_parse_media_type( pj_pool_t *pool,
1860 const pj_str_t *mime,
1861 pjsip_media_type *media_type)
1862{
1863 pj_str_t tmp;
1864 char *pos;
1865
Benny Prijonoac623b32006-07-03 15:19:31 +00001866 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001867
1868 pj_strdup_with_null(pool, &tmp, mime);
1869
1870 pos = pj_strchr(&tmp, '/');
1871 if (pos) {
1872 media_type->type.ptr = tmp.ptr;
1873 media_type->type.slen = (pos-tmp.ptr);
1874 media_type->subtype.ptr = pos+1;
1875 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1876 } else {
1877 media_type->type = tmp;
1878 }
1879}
1880
1881
1882/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001883 * Internal function to init transport selector from transport id.
1884 */
1885void pjsua_init_tpselector(pjsua_transport_id tp_id,
1886 pjsip_tpselector *sel)
1887{
1888 pjsua_transport_data *tpdata;
1889 unsigned flag;
1890
1891 pj_bzero(sel, sizeof(*sel));
1892 if (tp_id == PJSUA_INVALID_ID)
1893 return;
1894
Benny Prijonoa1e69682007-05-11 15:14:34 +00001895 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001896 tpdata = &pjsua_var.tpdata[tp_id];
1897
1898 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1899
1900 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1901 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1902 sel->u.transport = tpdata->data.tp;
1903 } else {
1904 sel->type = PJSIP_TPSELECTOR_LISTENER;
1905 sel->u.listener = tpdata->data.factory;
1906 }
1907}
1908
1909
Benny Prijono6ba8c542007-10-16 01:34:14 +00001910/* Callback upon NAT detection completion */
1911static void nat_detect_cb(void *user_data,
1912 const pj_stun_nat_detect_result *res)
1913{
1914 PJ_UNUSED_ARG(user_data);
1915
1916 pjsua_var.nat_in_progress = PJ_FALSE;
1917 pjsua_var.nat_status = res->status;
1918 pjsua_var.nat_type = res->nat_type;
1919
1920 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
1921 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
1922 }
1923}
1924
1925
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001926/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001927 * Detect NAT type.
1928 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00001929PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001930{
1931 pj_status_t status;
1932
Benny Prijono6ba8c542007-10-16 01:34:14 +00001933 if (pjsua_var.nat_in_progress)
1934 return PJ_SUCCESS;
1935
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001936 /* Make sure STUN server resolution has completed */
1937 status = pjsua_resolve_stun_server(PJ_TRUE);
1938 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001939 pjsua_var.nat_status = status;
1940 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001941 return status;
1942 }
1943
1944 /* Make sure we have STUN */
1945 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001946 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
1947 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001948 }
1949
Benny Prijono6ba8c542007-10-16 01:34:14 +00001950 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
1951 &pjsua_var.stun_cfg,
1952 NULL, &nat_detect_cb);
1953
1954 if (status != PJ_SUCCESS) {
1955 pjsua_var.nat_status = status;
1956 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
1957 return status;
1958 }
1959
1960 pjsua_var.nat_in_progress = PJ_TRUE;
1961
1962 return PJ_SUCCESS;
1963}
1964
1965
1966/*
1967 * Get NAT type.
1968 */
1969PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
1970{
1971 *type = pjsua_var.nat_type;
1972 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001973}
1974
1975
1976/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001977 * Verify that valid SIP url is given.
1978 */
1979PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1980{
1981 pjsip_uri *p;
1982 pj_pool_t *pool;
1983 char *url;
1984 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1985
1986 if (!len) return -1;
1987
1988 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1989 if (!pool) return -1;
1990
Benny Prijonoa1e69682007-05-11 15:14:34 +00001991 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001992 pj_ansi_strcpy(url, c_url);
1993
1994 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001995 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
1996 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
1997 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001998 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00001999 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002000
2001 pj_pool_release(pool);
2002 return p ? 0 : -1;
2003}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002004
2005
2006/*
2007 * This is a utility function to dump the stack states to log, using
2008 * verbosity level 3.
2009 */
2010PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2011{
2012 unsigned old_decor;
2013 unsigned i;
2014 char buf[1024];
2015
2016 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2017
2018 old_decor = pj_log_get_decor();
2019 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2020
2021 if (detail)
2022 pj_dump_config();
2023
2024 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2025
2026 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2027
2028 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2029 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2030 pjsua_call *call = &pjsua_var.calls[i];
2031 pjmedia_sock_info skinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002032 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002033
Benny Prijono4f093d22007-04-09 08:54:32 +00002034 /* MSVC complains about skinfo not being initialized */
2035 pj_bzero(&skinfo, sizeof(skinfo));
2036
Benny Prijonoda9785b2007-04-02 20:43:06 +00002037 pjmedia_transport_get_info(call->med_tp, &skinfo);
2038
Benny Prijono5186eae2007-12-03 14:38:25 +00002039 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002040 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijono5186eae2007-12-03 14:38:25 +00002041 pj_sockaddr_print(&skinfo.rtp_addr_name, addr_buf,
2042 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002043 }
2044
2045 pjsip_tsx_layer_dump(detail);
2046 pjsip_ua_dump(detail);
2047
2048
2049 /* Dump all invite sessions: */
2050 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2051
2052 if (pjsua_call_get_count() == 0) {
2053
2054 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2055
2056 } else {
2057 unsigned i;
2058
2059 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2060 if (pjsua_call_is_active(i)) {
2061 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
2062 PJ_LOG(3,(THIS_FILE, "%s", buf));
2063 }
2064 }
2065 }
2066
2067 /* Dump presence status */
2068 pjsua_pres_dump(detail);
2069
2070 pj_log_set_decor(old_decor);
2071 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2072}
2073