blob: 4adc191b90df2ac9ebe66f9fb595adb04fc6e22d [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono268ca612006-02-07 12:34:11 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000020#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000021#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono268ca612006-02-07 12:34:11 +000023
Benny Prijono84126ab2006-02-09 09:30:09 +000024#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000025
26
Benny Prijonobb995fd2009-08-12 11:03:23 +000027/* Internal prototypes */
28static void resolve_stun_entry(pjsua_stun_resolve *sess);
29
30
Benny Prijonoeebe9af2006-06-13 22:57:13 +000031/* PJSUA application instance. */
32struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000033
34
Benny Prijono44e88ea2007-10-30 15:42:35 +000035PJ_DEF(struct pjsua_data*) pjsua_get_var(void)
36{
37 return &pjsua_var;
38}
39
40
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041/* Display error */
42PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
43 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000044{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000045 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000046
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonod6e362a2008-07-19 17:53:47 +000048 PJ_LOG(1,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000049}
50
51
Benny Prijonoeebe9af2006-06-13 22:57:13 +000052static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000053{
54 unsigned i;
55
Benny Prijonoc97608e2007-03-23 16:34:20 +000056 pj_bzero(&pjsua_var, sizeof(pjsua_var));
57
Benny Prijonoeebe9af2006-06-13 22:57:13 +000058 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
59 pjsua_var.acc[i].index = i;
60
61 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
62 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000063
64 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijono6ba8c542007-10-16 01:34:14 +000065 pjsua_var.nat_status = PJ_EPENDING;
Benny Prijonobb995fd2009-08-12 11:03:23 +000066 pj_list_init(&pjsua_var.stun_res);
Benny Prijono29c8ca32010-06-22 06:02:13 +000067 pj_list_init(&pjsua_var.outbound_proxy);
Benny Prijonod64c1502009-08-17 10:42:55 +000068
69 pjsua_config_default(&pjsua_var.ua_cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000070}
Benny Prijonodc39fe82006-05-26 12:17:46 +000071
72
Benny Prijono1f61a8f2007-08-16 10:11:44 +000073PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
74{
75 pj_bzero(cfg, sizeof(*cfg));
76
77 cfg->msg_logging = PJ_TRUE;
78 cfg->level = 5;
79 cfg->console_level = 4;
80 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
Benny Prijonod6e362a2008-07-19 17:53:47 +000081 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE |
82 PJ_LOG_HAS_SPACE;
83#if defined(PJ_WIN32) && PJ_WIN32 != 0
84 cfg->decor |= PJ_LOG_HAS_COLOR;
85#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +000086}
87
88PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
89 pjsua_logging_config *dst,
90 const pjsua_logging_config *src)
91{
92 pj_memcpy(dst, src, sizeof(*src));
93 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
94}
95
96PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
97{
98 pj_bzero(cfg, sizeof(*cfg));
99
Benny Prijono6627f3e2009-02-10 11:08:54 +0000100 cfg->max_calls = ((PJSUA_MAX_CALLS) < 4) ? (PJSUA_MAX_CALLS) : 4;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000101 cfg->thread_cnt = 1;
Benny Prijono91a6a172007-10-31 08:59:29 +0000102 cfg->nat_type_in_sdp = 1;
Benny Prijonobb995fd2009-08-12 11:03:23 +0000103 cfg->stun_ignore_failure = PJ_TRUE;
Benny Prijono91d06b62008-09-20 12:16:56 +0000104 cfg->force_lr = PJ_TRUE;
Benny Prijonofe1bd342009-11-20 23:33:07 +0000105 cfg->enable_unsolicited_mwi = PJ_TRUE;
Benny Prijonod8179652008-01-23 20:39:07 +0000106#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
107 cfg->use_srtp = PJSUA_DEFAULT_USE_SRTP;
108 cfg->srtp_secure_signaling = PJSUA_DEFAULT_SRTP_SECURE_SIGNALING;
109#endif
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000110 cfg->hangup_forked_call = PJ_TRUE;
Nanang Izzuddin59dffb12009-08-11 12:42:38 +0000111
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000112 cfg->use_timer = PJSUA_SIP_TIMER_OPTIONAL;
Nanang Izzuddin65add622009-08-11 16:26:20 +0000113 pjsip_timer_setting_default(&cfg->timer_setting);
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000114}
115
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000116PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
117 pjsua_config *dst,
118 const pjsua_config *src)
119{
120 unsigned i;
121
122 pj_memcpy(dst, src, sizeof(*src));
123
124 for (i=0; i<src->outbound_proxy_cnt; ++i) {
125 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
126 &src->outbound_proxy[i]);
127 }
128
129 for (i=0; i<src->cred_count; ++i) {
130 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
131 }
132
133 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
134 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
135 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
Benny Prijonobb995fd2009-08-12 11:03:23 +0000136
137 for (i=0; i<src->stun_srv_cnt; ++i) {
138 pj_strdup_with_null(pool, &dst->stun_srv[i], &src->stun_srv[i]);
139 }
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000140}
141
142PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
143{
144 pj_bzero(msg_data, sizeof(*msg_data));
145 pj_list_init(&msg_data->hdr_list);
Benny Prijono1c1d7342010-08-01 09:48:51 +0000146 pjsip_media_type_init(&msg_data->multipart_ctype, NULL, NULL);
147 pj_list_init(&msg_data->multipart_parts);
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000148}
149
150PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
151{
152 pj_bzero(cfg, sizeof(*cfg));
153 pjsip_tls_setting_default(&cfg->tls_setting);
154}
155
156PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
157 pjsua_transport_config *dst,
158 const pjsua_transport_config *src)
159{
160 PJ_UNUSED_ARG(pool);
161 pj_memcpy(dst, src, sizeof(*src));
162}
163
164PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
165{
166 pj_bzero(cfg, sizeof(*cfg));
167
168 cfg->reg_timeout = PJSUA_REG_INTERVAL;
Benny Prijono384dab42009-10-14 01:58:04 +0000169 cfg->unreg_timeout = PJSUA_UNREG_TIMEOUT;
Benny Prijonofe50c9e2009-10-12 07:44:14 +0000170 pjsip_publishc_opt_default(&cfg->publish_opt);
Benny Prijono534a9ba2009-10-13 14:01:59 +0000171 cfg->unpublish_max_wait_time_msec = PJSUA_UNPUBLISH_MAX_WAIT_TIME_MSEC;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000172 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000173 cfg->allow_contact_rewrite = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000174 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Nanang Izzuddin742ef4b2010-09-07 09:36:15 +0000175 cfg->use_timer = pjsua_var.ua_cfg.use_timer;
Nanang Izzuddin65add622009-08-11 16:26:20 +0000176 cfg->timer_setting = pjsua_var.ua_cfg.timer_setting;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000177 cfg->ka_interval = 15;
178 cfg->ka_data = pj_str("\r\n");
Benny Prijonod8179652008-01-23 20:39:07 +0000179#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
180 cfg->use_srtp = pjsua_var.ua_cfg.use_srtp;
181 cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling;
Nanang Izzuddind89cc3a2010-05-13 05:22:51 +0000182 cfg->srtp_optional_dup_offer = pjsua_var.ua_cfg.srtp_optional_dup_offer;
Benny Prijonod8179652008-01-23 20:39:07 +0000183#endif
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +0000184 cfg->reg_retry_interval = PJSUA_REG_RETRY_INTERVAL;
Benny Prijonoc6d5fdc2010-06-20 08:58:26 +0000185 cfg->contact_rewrite_method = PJSUA_CONTACT_REWRITE_METHOD;
Benny Prijono29c8ca32010-06-22 06:02:13 +0000186 cfg->reg_use_proxy = PJSUA_REG_USE_OUTBOUND_PROXY |
187 PJSUA_REG_USE_ACC_PROXY;
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000188#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
189 cfg->use_stream_ka = (PJMEDIA_STREAM_ENABLE_KA != 0);
190#endif
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +0000191 pj_list_init(&cfg->reg_hdr_list);
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000192}
193
194PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
195{
196 pj_bzero(cfg, sizeof(*cfg));
197}
198
199PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
200{
201 pj_bzero(cfg, sizeof(*cfg));
202
203 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono50f19b32008-03-11 13:15:43 +0000204 cfg->snd_clock_rate = 0;
Benny Prijono7d60d052008-03-29 12:24:20 +0000205 cfg->channel_count = 1;
Benny Prijono37c710b2008-01-10 12:09:26 +0000206 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
207 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000208 cfg->has_ioqueue = PJ_TRUE;
209 cfg->thread_cnt = 1;
210 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
211 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
212 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
Benny Prijono10454dc2009-02-21 14:21:59 +0000213 cfg->snd_rec_latency = PJMEDIA_SND_DEFAULT_REC_LATENCY;
214 cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000215 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
Benny Prijonof798e502009-03-09 13:08:16 +0000216 cfg->snd_auto_close_time = 1;
Benny Prijonof76e1392008-06-06 14:51:48 +0000217
Benny Prijono329d6382009-05-29 13:04:03 +0000218 cfg->ice_max_host_cands = -1;
219 pj_ice_sess_options_default(&cfg->ice_opt);
220
Benny Prijonof76e1392008-06-06 14:51:48 +0000221 cfg->turn_conn_type = PJ_TURN_TP_UDP;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000222}
223
Benny Prijonodc39fe82006-05-26 12:17:46 +0000224
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000225/*****************************************************************************
226 * This is a very simple PJSIP module, whose sole purpose is to display
227 * incoming and outgoing messages to log. This module will have priority
228 * higher than transport layer, which means:
229 *
230 * - incoming messages will come to this module first before reaching
231 * transaction layer.
232 *
233 * - outgoing messages will come to this module last, after the message
234 * has been 'printed' to contiguous buffer by transport layer and
235 * appropriate transport instance has been decided for this message.
236 *
237 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000238
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000239/* Notification on incoming messages */
240static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
241{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000242 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
243 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000244 "--end msg--",
245 rdata->msg_info.len,
246 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000247 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000248 rdata->pkt_info.src_name,
249 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000250 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000251 rdata->msg_info.msg_buf));
252
253 /* Always return false, otherwise messages will not get processed! */
254 return PJ_FALSE;
255}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000256
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000257/* Notification on outgoing messages */
258static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
259{
260
261 /* Important note:
262 * tp_info field is only valid after outgoing messages has passed
263 * transport layer. So don't try to access tp_info when the module
264 * has lower priority than transport layer.
265 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000266
Benny Prijonob4a17c92006-07-10 14:40:21 +0000267 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
268 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000269 "--end msg--",
270 (tdata->buf.cur - tdata->buf.start),
271 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000272 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000273 tdata->tp_info.dst_name,
274 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000275 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000276 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000277
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000278 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000279 return PJ_SUCCESS;
280}
281
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000282/* The module instance. */
283static pjsip_module pjsua_msg_logger =
284{
285 NULL, NULL, /* prev, next. */
286 { "mod-pjsua-log", 13 }, /* Name. */
287 -1, /* Id */
288 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
289 NULL, /* load() */
290 NULL, /* start() */
291 NULL, /* stop() */
292 NULL, /* unload() */
293 &logging_on_rx_msg, /* on_rx_request() */
294 &logging_on_rx_msg, /* on_rx_response() */
295 &logging_on_tx_msg, /* on_tx_request. */
296 &logging_on_tx_msg, /* on_tx_response() */
297 NULL, /* on_tsx_state() */
298
299};
300
301
302/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000303 * Another simple module to handle incoming OPTIONS request
304 */
305
306/* Notification on incoming request */
307static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
308{
309 pjsip_tx_data *tdata;
310 pjsip_response_addr res_addr;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000311 pjmedia_transport_info tpinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000312 pjmedia_sdp_session *sdp;
313 const pjsip_hdr *cap_hdr;
314 pj_status_t status;
315
316 /* Only want to handle OPTIONS requests */
317 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000318 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000319 {
320 return PJ_FALSE;
321 }
322
Benny Prijono384dab42009-10-14 01:58:04 +0000323 /* Don't want to handle if shutdown is in progress */
324 if (pjsua_var.thread_quit_flag) {
325 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
326 PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
327 NULL, NULL);
328 return PJ_TRUE;
329 }
330
Benny Prijono56315612006-07-18 14:39:40 +0000331 /* Create basic response. */
332 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
333 &tdata);
334 if (status != PJ_SUCCESS) {
335 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
336 return PJ_TRUE;
337 }
338
339 /* Add Allow header */
340 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
341 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000342 pjsip_msg_add_hdr(tdata->msg,
343 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000344 }
345
346 /* Add Accept header */
347 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
348 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000349 pjsip_msg_add_hdr(tdata->msg,
350 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000351 }
352
353 /* Add Supported header */
354 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
355 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000356 pjsip_msg_add_hdr(tdata->msg,
357 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000358 }
359
360 /* Add Allow-Events header from the evsub module */
361 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
362 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000363 pjsip_msg_add_hdr(tdata->msg,
364 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000365 }
366
367 /* Add User-Agent header */
368 if (pjsua_var.ua_cfg.user_agent.slen) {
369 const pj_str_t USER_AGENT = { "User-Agent", 10};
370 pjsip_hdr *h;
371
372 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
373 &USER_AGENT,
374 &pjsua_var.ua_cfg.user_agent);
375 pjsip_msg_add_hdr(tdata->msg, h);
376 }
377
Benny Prijonoa248b952009-08-12 22:28:47 +0000378 /* Get media socket info, make sure transport is ready */
379 if (pjsua_var.calls[0].med_tp) {
380 pjmedia_transport_info_init(&tpinfo);
381 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000382
Benny Prijonoa248b952009-08-12 22:28:47 +0000383 /* Add SDP body, using call0's RTP address */
384 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
385 &tpinfo.sock_info, &sdp);
386 if (status == PJ_SUCCESS) {
387 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
388 }
Benny Prijono56315612006-07-18 14:39:40 +0000389 }
390
391 /* Send response statelessly */
392 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
393 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
394 if (status != PJ_SUCCESS)
395 pjsip_tx_data_dec_ref(tdata);
396
397 return PJ_TRUE;
398}
399
400
401/* The module instance. */
402static pjsip_module pjsua_options_handler =
403{
404 NULL, NULL, /* prev, next. */
405 { "mod-pjsua-options", 17 }, /* Name. */
406 -1, /* Id */
407 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
408 NULL, /* load() */
409 NULL, /* start() */
410 NULL, /* stop() */
411 NULL, /* unload() */
412 &options_on_rx_request, /* on_rx_request() */
413 NULL, /* on_rx_response() */
414 NULL, /* on_tx_request. */
415 NULL, /* on_tx_response() */
416 NULL, /* on_tsx_state() */
417
418};
419
420
421/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000422 * These two functions are the main callbacks registered to PJSIP stack
423 * to receive SIP request and response messages that are outside any
424 * dialogs and any transactions.
425 */
Benny Prijono268ca612006-02-07 12:34:11 +0000426
427/*
428 * Handler for receiving incoming requests.
429 *
430 * This handler serves multiple purposes:
431 * - it receives requests outside dialogs.
432 * - it receives requests inside dialogs, when the requests are
433 * unhandled by other dialog usages. Example of these
434 * requests are: MESSAGE.
435 */
436static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
437{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000438 pj_bool_t processed = PJ_FALSE;
439
440 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000441
Benny Prijono84126ab2006-02-09 09:30:09 +0000442 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000443
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000445 }
446
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000447 PJSUA_UNLOCK();
448
449 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000450}
451
452
453/*
454 * Handler for receiving incoming responses.
455 *
456 * This handler serves multiple purposes:
457 * - it receives strayed responses (i.e. outside any dialog and
458 * outside any transactions).
459 * - it receives responses coming to a transaction, when pjsua
460 * module is set as transaction user for the transaction.
461 * - it receives responses inside a dialog, when these responses
462 * are unhandled by other dialog usages.
463 */
464static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
465{
466 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000467 return PJ_FALSE;
468}
469
470
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471/*****************************************************************************
472 * Logging.
473 */
474
475/* Log callback */
476static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000477{
Benny Prijono572d4852006-11-23 21:50:02 +0000478 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479
480 if (pjsua_var.log_file) {
481 pj_ssize_t size = len;
482 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000483 /* This will slow things down considerably! Don't do it!
484 pj_file_flush(pjsua_var.log_file);
485 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000486 }
487
Benny Prijono572d4852006-11-23 21:50:02 +0000488 if (level <= (int)pjsua_var.log_cfg.console_level) {
489 if (pjsua_var.log_cfg.cb)
490 (*pjsua_var.log_cfg.cb)(level, buffer, len);
491 else
492 pj_log_write(level, buffer, len);
493 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000494}
495
496
497/*
498 * Application can call this function at any time (after pjsua_create(), of
499 * course) to change logging settings.
500 */
501PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
502{
503 pj_status_t status;
504
505 /* Save config. */
506 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
507
508 /* Redirect log function to ours */
509 pj_log_set_log_func( &log_writer );
510
Benny Prijono9cb09a22007-08-30 09:35:10 +0000511 /* Set decor */
512 pj_log_set_decor(pjsua_var.log_cfg.decor);
513
Benny Prijono4190cf92008-01-18 13:25:05 +0000514 /* Set log level */
515 pj_log_set_level(pjsua_var.log_cfg.level);
516
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000517 /* Close existing file, if any */
518 if (pjsua_var.log_file) {
519 pj_file_close(pjsua_var.log_file);
520 pjsua_var.log_file = NULL;
521 }
522
523 /* If output log file is desired, create the file: */
524 if (pjsua_var.log_cfg.log_filename.slen) {
Benny Prijonodbe3f4b2009-05-07 16:56:04 +0000525 unsigned flags = PJ_O_WRONLY;
526 flags |= pjsua_var.log_cfg.log_file_flags;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000527 status = pj_file_open(pjsua_var.pool,
528 pjsua_var.log_cfg.log_filename.ptr,
Benny Prijonodbe3f4b2009-05-07 16:56:04 +0000529 flags,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000530 &pjsua_var.log_file);
531
532 if (status != PJ_SUCCESS) {
533 pjsua_perror(THIS_FILE, "Error creating log file", status);
534 return status;
535 }
536 }
537
538 /* Unregister msg logging if it's previously registered */
539 if (pjsua_msg_logger.id >= 0) {
540 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
541 pjsua_msg_logger.id = -1;
542 }
543
544 /* Enable SIP message logging */
545 if (pjsua_var.log_cfg.msg_logging)
546 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
547
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000548 return PJ_SUCCESS;
549}
550
551
552/*****************************************************************************
553 * PJSUA Base API.
554 */
555
556/* Worker thread function. */
557static int worker_thread(void *arg)
558{
559 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000560
Benny Prijono268ca612006-02-07 12:34:11 +0000561 PJ_UNUSED_ARG(arg);
562
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000563 while (!pjsua_var.thread_quit_flag) {
564 int count;
565
566 count = pjsua_handle_events(TIMEOUT);
567 if (count < 0)
568 pj_thread_sleep(TIMEOUT);
569 }
Benny Prijono268ca612006-02-07 12:34:11 +0000570
571 return 0;
572}
573
Benny Prijonodc39fe82006-05-26 12:17:46 +0000574
Benny Prijono8389c312008-02-21 21:36:34 +0000575/* Init random seed */
576static void init_random_seed(void)
577{
578 pj_sockaddr addr;
579 const pj_str_t *hostname;
580 pj_uint32_t pid;
581 pj_time_val t;
582 unsigned seed=0;
583
584 /* Add hostname */
585 hostname = pj_gethostname();
586 seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen);
587
588 /* Add primary IP address */
589 if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS)
590 seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4);
591
592 /* Get timeofday */
593 pj_gettimeofday(&t);
594 seed = pj_hash_calc(seed, &t, sizeof(t));
595
596 /* Add PID */
597 pid = pj_getpid();
598 seed = pj_hash_calc(seed, &pid, sizeof(pid));
599
600 /* Init random seed */
601 pj_srand(seed);
602}
603
Benny Prijono268ca612006-02-07 12:34:11 +0000604/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000605 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000606 */
607PJ_DEF(pj_status_t) pjsua_create(void)
608{
609 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000610
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611 /* Init pjsua data */
612 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000613
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000614 /* Set default logging settings */
615 pjsua_logging_config_default(&pjsua_var.log_cfg);
616
617 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000618 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000619 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
620
Benny Prijono8389c312008-02-21 21:36:34 +0000621 /* Init random seed */
622 init_random_seed();
Benny Prijono268ca612006-02-07 12:34:11 +0000623
Benny Prijonofccab712006-02-22 22:23:22 +0000624 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000625 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000626 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000627
Benny Prijonoec921342007-03-24 13:00:30 +0000628 /* Init PJNATH */
629 status = pjnath_init();
630 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000631
Benny Prijono094d3ad2006-11-21 08:41:00 +0000632 /* Set default sound device ID */
Benny Prijono96e74f32009-02-22 12:00:12 +0000633 pjsua_var.cap_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
634 pjsua_var.play_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
Benny Prijono094d3ad2006-11-21 08:41:00 +0000635
Benny Prijono268ca612006-02-07 12:34:11 +0000636 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000637 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000638
639 /* Create memory pool for application. */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000640 pjsua_var.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641
642 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000643
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000644 /* Create mutex */
645 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
646 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000647 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000648 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000649 return status;
650 }
651
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000652 /* Must create SIP endpoint to initialize SIP parser. The parser
653 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000654 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000655 status = pjsip_endpt_create(&pjsua_var.cp.factory,
656 pj_gethostname()->ptr,
657 &pjsua_var.endpt);
658 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000659
660
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000661 return PJ_SUCCESS;
662}
663
664
665/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000666 * Initialize pjsua with the specified settings. All the settings are
667 * optional, and the default values will be used when the config is not
668 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000669 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000670PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
671 const pjsua_logging_config *log_cfg,
672 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000673{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000674 pjsua_config default_cfg;
675 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000676 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000677 pjsip_ua_init_param ua_init_param;
Benny Prijono29c8ca32010-06-22 06:02:13 +0000678 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000679 pj_status_t status;
680
681
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000682 /* Create default configurations when the config is not supplied */
683
684 if (ua_cfg == NULL) {
685 pjsua_config_default(&default_cfg);
686 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000687 }
688
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000689 if (media_cfg == NULL) {
690 pjsua_media_config_default(&default_media_cfg);
691 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000692 }
693
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000694 /* Initialize logging first so that info/errors can be captured */
695 if (log_cfg) {
696 status = pjsua_reconfigure_logging(log_cfg);
Benny Prijono329d6382009-05-29 13:04:03 +0000697 if (status != PJ_SUCCESS)
698 return status;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000699 }
700
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000701 /* If nameserver is configured, create DNS resolver instance and
702 * set it to be used by SIP resolver.
703 */
704 if (ua_cfg->nameserver_count) {
705#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000706 unsigned i;
707
708 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000709 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
710 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000711 if (status != PJ_SUCCESS) {
712 pjsua_perror(THIS_FILE, "Error creating resolver", status);
713 return status;
714 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000715
716 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000717 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
718 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000719 ua_cfg->nameserver, NULL);
720 if (status != PJ_SUCCESS) {
721 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
722 return status;
723 }
724
725 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000726 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000727 if (status != PJ_SUCCESS) {
728 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
729 return status;
730 }
731
732 /* Print nameservers */
733 for (i=0; i<ua_cfg->nameserver_count; ++i) {
734 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
735 (int)ua_cfg->nameserver[i].slen,
736 ua_cfg->nameserver[i].ptr));
737 }
738#else
739 PJ_LOG(2,(THIS_FILE,
740 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
741#endif
742 }
743
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000744 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000745
746 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000747 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
748 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000749
Benny Prijonodc39fe82006-05-26 12:17:46 +0000750
751 /* Initialize UA layer module: */
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000752 pj_bzero(&ua_init_param, sizeof(ua_init_param));
753 if (ua_cfg->hangup_forked_call) {
754 ua_init_param.on_dlg_forked = &on_dlg_forked;
755 }
756 status = pjsip_ua_init_module( pjsua_var.endpt, &ua_init_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000757 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000758
Benny Prijonodc39fe82006-05-26 12:17:46 +0000759
Benny Prijono053f5222006-11-11 16:16:04 +0000760 /* Initialize Replaces support. */
761 status = pjsip_replaces_init_module( pjsua_var.endpt );
762 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
763
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000764 /* Initialize 100rel support */
765 status = pjsip_100rel_init_module(pjsua_var.endpt);
766 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000767
Nanang Izzuddin59dffb12009-08-11 12:42:38 +0000768 /* Initialize session timer support */
769 status = pjsip_timer_init_module(pjsua_var.endpt);
770 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
771
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000772 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000773 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000774 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000775 {
776 NULL, NULL, /* prev, next. */
777 { "mod-pjsua", 9 }, /* Name. */
778 -1, /* Id */
779 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
780 NULL, /* load() */
781 NULL, /* start() */
782 NULL, /* stop() */
783 NULL, /* unload() */
784 &mod_pjsua_on_rx_request, /* on_rx_request() */
785 &mod_pjsua_on_rx_response, /* on_rx_response() */
786 NULL, /* on_tx_request. */
787 NULL, /* on_tx_response() */
788 NULL, /* on_tsx_state() */
789 };
790
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000791 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000792
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000793 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
794 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000795 }
796
Benny Prijono29c8ca32010-06-22 06:02:13 +0000797 /* Parse outbound proxies */
798 for (i=0; i<ua_cfg->outbound_proxy_cnt; ++i) {
799 pj_str_t tmp;
800 pj_str_t hname = { "Route", 5};
801 pjsip_route_hdr *r;
802
803 pj_strdup_with_null(pjsua_var.pool, &tmp, &ua_cfg->outbound_proxy[i]);
804
805 r = (pjsip_route_hdr*)
806 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr,
807 (unsigned)tmp.slen, NULL);
808 if (r == NULL) {
809 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
810 PJSIP_EINVALIDURI);
811 return PJSIP_EINVALIDURI;
812 }
813
814 if (pjsua_var.ua_cfg.force_lr) {
815 pjsip_sip_uri *sip_url;
816 if (!PJSIP_URI_SCHEME_IS_SIP(r->name_addr.uri) &&
817 !PJSIP_URI_SCHEME_IS_SIP(r->name_addr.uri))
818 {
819 return PJSIP_EINVALIDSCHEME;
820 }
821 sip_url = (pjsip_sip_uri*)r->name_addr.uri;
822 sip_url->lr_param = 1;
823 }
824
825 pj_list_push_back(&pjsua_var.outbound_proxy, r);
826 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000827
Benny Prijonodc39fe82006-05-26 12:17:46 +0000828
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829 /* Initialize PJSUA call subsystem: */
830 status = pjsua_call_subsys_init(ua_cfg);
831 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000832 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000833
Benny Prijonobb995fd2009-08-12 11:03:23 +0000834 /* Convert deprecated STUN settings */
835 if (pjsua_var.ua_cfg.stun_srv_cnt==0) {
836 if (pjsua_var.ua_cfg.stun_domain.slen) {
837 pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] =
838 pjsua_var.ua_cfg.stun_domain;
839 }
840 if (pjsua_var.ua_cfg.stun_host.slen) {
841 pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] =
842 pjsua_var.ua_cfg.stun_host;
843 }
844 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000845
Benny Prijonoc97608e2007-03-23 16:34:20 +0000846 /* Start resolving STUN server */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000847 status = resolve_stun_server(PJ_FALSE);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000848 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
849 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
850 return status;
851 }
852
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000853 /* Initialize PJSUA media subsystem */
854 status = pjsua_media_subsys_init(media_cfg);
855 if (status != PJ_SUCCESS)
856 goto on_error;
857
Benny Prijonodc39fe82006-05-26 12:17:46 +0000858
859 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000860 status = pjsip_evsub_init_module(pjsua_var.endpt);
861 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000862
Benny Prijonodc39fe82006-05-26 12:17:46 +0000863
864 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000865 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
866 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000867
Benny Prijono4dd961b2009-10-26 11:21:37 +0000868 /* Initialize MWI support */
869 status = pjsip_mwi_init_module(pjsua_var.endpt, pjsip_evsub_instance());
870
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000871 /* Init PUBLISH module */
872 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000873
874 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000875 status = pjsip_xfer_init_module( pjsua_var.endpt );
876 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000877
878 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000879 status = pjsua_pres_init();
880 if (status != PJ_SUCCESS)
881 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000882
883 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000884 status = pjsua_im_init();
885 if (status != PJ_SUCCESS)
886 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000887
Benny Prijonoc8141a82006-08-20 09:12:19 +0000888 /* Register OPTIONS handler */
889 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
890
891 /* Add OPTIONS in Allow header */
892 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
893 NULL, 1, &STR_OPTIONS);
894
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000895 /* Start worker thread if needed. */
896 if (pjsua_var.ua_cfg.thread_cnt) {
897 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000898
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000899 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
900 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000901
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000902 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
903 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
904 NULL, 0, 0, &pjsua_var.thread[i]);
905 if (status != PJ_SUCCESS)
906 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000907 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000908 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
909 pjsua_var.ua_cfg.thread_cnt));
910 } else {
911 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000912 }
913
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000914 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000915
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000916 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000917 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000918
Benny Prijono268ca612006-02-07 12:34:11 +0000919 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000920
921on_error:
922 pjsua_destroy();
923 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000924}
925
926
Benny Prijono834aee32006-02-19 01:38:06 +0000927/* Sleep with polling */
928static void busy_sleep(unsigned msec)
929{
930 pj_time_val timeout, now;
931
932 pj_gettimeofday(&timeout);
933 timeout.msec += msec;
934 pj_time_val_normalize(&timeout);
935
936 do {
Nanang Izzuddin90b83202009-03-02 15:48:45 +0000937 int i;
938 i = msec / 10;
939 while (pjsua_handle_events(10) > 0 && i > 0)
940 --i;
Benny Prijono834aee32006-02-19 01:38:06 +0000941 pj_gettimeofday(&now);
942 } while (PJ_TIME_VAL_LT(now, timeout));
943}
944
Benny Prijonobb995fd2009-08-12 11:03:23 +0000945/* Internal function to destroy STUN resolution session
946 * (pj_stun_resolve).
Benny Prijonoebbf6892007-03-24 17:37:25 +0000947 */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000948static void destroy_stun_resolve(pjsua_stun_resolve *sess)
Benny Prijonoebbf6892007-03-24 17:37:25 +0000949{
Benny Prijonobb995fd2009-08-12 11:03:23 +0000950 PJSUA_LOCK();
951 pj_list_erase(sess);
952 PJSUA_UNLOCK();
Benny Prijonoebbf6892007-03-24 17:37:25 +0000953
Benny Prijonobb995fd2009-08-12 11:03:23 +0000954 pj_assert(sess->stun_sock==NULL);
955 pj_pool_release(sess->pool);
956}
Benny Prijonoebbf6892007-03-24 17:37:25 +0000957
Benny Prijonobb995fd2009-08-12 11:03:23 +0000958/* This is the internal function to be called when STUN resolution
959 * session (pj_stun_resolve) has completed.
960 */
961static void stun_resolve_complete(pjsua_stun_resolve *sess)
962{
963 pj_stun_resolve_result result;
964
965 pj_bzero(&result, sizeof(result));
966 result.token = sess->token;
967 result.status = sess->status;
968 result.name = sess->srv[sess->idx];
969 pj_memcpy(&result.addr, &sess->addr, sizeof(result.addr));
970
971 if (result.status == PJ_SUCCESS) {
972 char addr[PJ_INET6_ADDRSTRLEN+10];
973 pj_sockaddr_print(&result.addr, addr, sizeof(addr), 3);
974 PJ_LOG(4,(THIS_FILE,
975 "STUN resolution success, using %.*s, address is %s",
976 (int)sess->srv[sess->idx].slen,
977 sess->srv[sess->idx].ptr,
978 addr));
979 } else {
980 char errmsg[PJ_ERR_MSG_SIZE];
981 pj_strerror(result.status, errmsg, sizeof(errmsg));
982 PJ_LOG(1,(THIS_FILE, "STUN resolution failed: %s", errmsg));
983 }
984
985 sess->cb(&result);
986
987 if (!sess->blocking) {
988 destroy_stun_resolve(sess);
989 }
990}
991
992/* This is the callback called by the STUN socket (pj_stun_sock)
993 * to report it's state. We use this as part of testing the
994 * STUN server.
995 */
996static pj_bool_t test_stun_on_status(pj_stun_sock *stun_sock,
997 pj_stun_sock_op op,
998 pj_status_t status)
999{
1000 pjsua_stun_resolve *sess;
1001
Nanang Izzuddinae1c6152009-08-17 16:30:04 +00001002 sess = (pjsua_stun_resolve*) pj_stun_sock_get_user_data(stun_sock);
Benny Prijonobb995fd2009-08-12 11:03:23 +00001003 pj_assert(stun_sock == sess->stun_sock);
1004
Benny Prijonoebbf6892007-03-24 17:37:25 +00001005 if (status != PJ_SUCCESS) {
Benny Prijonobb995fd2009-08-12 11:03:23 +00001006 char errmsg[PJ_ERR_MSG_SIZE];
1007 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonoebbf6892007-03-24 17:37:25 +00001008
Benny Prijonobb995fd2009-08-12 11:03:23 +00001009 PJ_LOG(4,(THIS_FILE, "STUN resolution for %.*s failed: %s",
1010 (int)sess->srv[sess->idx].slen,
1011 sess->srv[sess->idx].ptr, errmsg));
Benny Prijonoebbf6892007-03-24 17:37:25 +00001012
Benny Prijonobb995fd2009-08-12 11:03:23 +00001013 sess->status = status;
1014
1015 pj_stun_sock_destroy(stun_sock);
1016 sess->stun_sock = NULL;
1017
1018 ++sess->idx;
1019 resolve_stun_entry(sess);
1020
1021 return PJ_FALSE;
1022
1023 } else if (op == PJ_STUN_SOCK_BINDING_OP) {
1024 pj_stun_sock_info ssi;
1025
1026 pj_stun_sock_get_info(stun_sock, &ssi);
1027 pj_memcpy(&sess->addr, &ssi.srv_addr, sizeof(sess->addr));
1028
1029 sess->status = PJ_SUCCESS;
1030 pj_stun_sock_destroy(stun_sock);
1031 sess->stun_sock = NULL;
1032
1033 stun_resolve_complete(sess);
1034
1035 return PJ_FALSE;
1036
1037 } else
1038 return PJ_TRUE;
1039
1040}
1041
1042/* This is an internal function to resolve and test current
1043 * server entry in pj_stun_resolve session. It is called by
1044 * pjsua_resolve_stun_servers() and test_stun_on_status() above
1045 */
1046static void resolve_stun_entry(pjsua_stun_resolve *sess)
1047{
1048 /* Loop while we have entry to try */
1049 for (; sess->idx < sess->count; ++sess->idx) {
1050 const int af = pj_AF_INET();
1051 pj_str_t hostpart;
1052 pj_uint16_t port;
1053 pj_stun_sock_cb stun_sock_cb;
1054
1055 pj_assert(sess->idx < sess->count);
1056
1057 /* Parse the server entry into host:port */
1058 sess->status = pj_sockaddr_parse2(af, 0, &sess->srv[sess->idx],
1059 &hostpart, &port, NULL);
1060 if (sess->status != PJ_SUCCESS) {
1061 PJ_LOG(2,(THIS_FILE, "Invalid STUN server entry %.*s",
1062 (int)sess->srv[sess->idx].slen,
1063 sess->srv[sess->idx].ptr));
1064 continue;
Benny Prijonoebbf6892007-03-24 17:37:25 +00001065 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001066
1067 /* Use default port if not specified */
1068 if (port == 0)
1069 port = PJ_STUN_PORT;
1070
1071 pj_assert(sess->stun_sock == NULL);
1072
1073 PJ_LOG(4,(THIS_FILE, "Trying STUN server %.*s (%d of %d)..",
1074 (int)sess->srv[sess->idx].slen,
1075 sess->srv[sess->idx].ptr,
1076 sess->idx+1, sess->count));
1077
1078 /* Use STUN_sock to test this entry */
1079 pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb));
1080 stun_sock_cb.on_status = &test_stun_on_status;
1081 sess->status = pj_stun_sock_create(&pjsua_var.stun_cfg, "stunresolve",
1082 pj_AF_INET(), &stun_sock_cb,
1083 NULL, sess, &sess->stun_sock);
1084 if (sess->status != PJ_SUCCESS) {
1085 char errmsg[PJ_ERR_MSG_SIZE];
1086 pj_strerror(sess->status, errmsg, sizeof(errmsg));
1087 PJ_LOG(4,(THIS_FILE,
1088 "Error creating STUN socket for %.*s: %s",
1089 (int)sess->srv[sess->idx].slen,
1090 sess->srv[sess->idx].ptr, errmsg));
1091
1092 continue;
1093 }
1094
1095 sess->status = pj_stun_sock_start(sess->stun_sock, &hostpart,
1096 port, pjsua_var.resolver);
1097 if (sess->status != PJ_SUCCESS) {
1098 char errmsg[PJ_ERR_MSG_SIZE];
1099 pj_strerror(sess->status, errmsg, sizeof(errmsg));
1100 PJ_LOG(4,(THIS_FILE,
1101 "Error starting STUN socket for %.*s: %s",
1102 (int)sess->srv[sess->idx].slen,
1103 sess->srv[sess->idx].ptr, errmsg));
1104
1105 pj_stun_sock_destroy(sess->stun_sock);
1106 sess->stun_sock = NULL;
1107 continue;
1108 }
1109
1110 /* Done for now, testing will resume/complete asynchronously in
1111 * stun_sock_cb()
1112 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001113 return;
1114 }
1115
Benny Prijonobb995fd2009-08-12 11:03:23 +00001116 if (sess->idx >= sess->count) {
1117 /* No more entries to try */
1118 PJ_ASSERT_ON_FAIL(sess->status != PJ_SUCCESS,
1119 sess->status = PJ_EUNKNOWN);
1120 stun_resolve_complete(sess);
1121 }
1122}
Benny Prijonoebbf6892007-03-24 17:37:25 +00001123
Benny Prijonoebbf6892007-03-24 17:37:25 +00001124
Benny Prijonobb995fd2009-08-12 11:03:23 +00001125/*
1126 * Resolve STUN server.
1127 */
1128PJ_DEF(pj_status_t) pjsua_resolve_stun_servers( unsigned count,
1129 pj_str_t srv[],
1130 pj_bool_t wait,
1131 void *token,
1132 pj_stun_resolve_cb cb)
1133{
1134 pj_pool_t *pool;
1135 pjsua_stun_resolve *sess;
1136 pj_status_t status;
1137 unsigned i;
1138
1139 PJ_ASSERT_RETURN(count && srv && cb, PJ_EINVAL);
1140
1141 pool = pjsua_pool_create("stunres", 256, 256);
1142 if (!pool)
1143 return PJ_ENOMEM;
1144
1145 sess = PJ_POOL_ZALLOC_T(pool, pjsua_stun_resolve);
1146 sess->pool = pool;
1147 sess->token = token;
1148 sess->cb = cb;
1149 sess->count = count;
1150 sess->blocking = wait;
1151 sess->status = PJ_EPENDING;
1152 sess->srv = (pj_str_t*) pj_pool_calloc(pool, count, sizeof(pj_str_t));
1153 for (i=0; i<count; ++i) {
1154 pj_strdup(pool, &sess->srv[i], &srv[i]);
Benny Prijonoebbf6892007-03-24 17:37:25 +00001155 }
1156
Benny Prijonobb995fd2009-08-12 11:03:23 +00001157 PJSUA_LOCK();
1158 pj_list_push_back(&pjsua_var.stun_res, sess);
1159 PJSUA_UNLOCK();
1160
1161 resolve_stun_entry(sess);
1162
1163 if (!wait)
1164 return PJ_SUCCESS;
1165
1166 while (sess->status == PJ_EPENDING) {
1167 pjsua_handle_events(50);
1168 }
1169
1170 status = sess->status;
1171 destroy_stun_resolve(sess);
1172
1173 return status;
1174}
1175
1176/*
1177 * Cancel pending STUN resolution.
1178 */
1179PJ_DEF(pj_status_t) pjsua_cancel_stun_resolution( void *token,
1180 pj_bool_t notify_cb)
1181{
1182 pjsua_stun_resolve *sess;
1183 unsigned cancelled_count = 0;
1184
1185 PJSUA_LOCK();
1186 sess = pjsua_var.stun_res.next;
1187 while (sess != &pjsua_var.stun_res) {
1188 pjsua_stun_resolve *next = sess->next;
1189
1190 if (sess->token == token) {
1191 if (notify_cb) {
1192 pj_stun_resolve_result result;
1193
1194 pj_bzero(&result, sizeof(result));
1195 result.token = token;
1196 result.status = PJ_ECANCELLED;
1197
1198 sess->cb(&result);
1199 }
1200
1201 destroy_stun_resolve(sess);
1202 ++cancelled_count;
1203 }
1204
1205 sess = next;
1206 }
1207 PJSUA_UNLOCK();
1208
1209 return cancelled_count ? PJ_SUCCESS : PJ_ENOTFOUND;
1210}
1211
1212static void internal_stun_resolve_cb(const pj_stun_resolve_result *result)
1213{
1214 pjsua_var.stun_status = result->status;
1215 if (result->status == PJ_SUCCESS) {
1216 pj_memcpy(&pjsua_var.stun_srv, &result->addr, sizeof(result->addr));
1217 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001218}
1219
Benny Prijono268ca612006-02-07 12:34:11 +00001220/*
Benny Prijonoc97608e2007-03-23 16:34:20 +00001221 * Resolve STUN server.
1222 */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001223pj_status_t resolve_stun_server(pj_bool_t wait)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001224{
1225 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
Benny Prijonobb995fd2009-08-12 11:03:23 +00001226 pj_status_t status;
1227
Benny Prijonoc97608e2007-03-23 16:34:20 +00001228 /* Initialize STUN configuration */
1229 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
1230 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
1231 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
1232
1233 /* Start STUN server resolution */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001234 if (pjsua_var.ua_cfg.stun_srv_cnt) {
1235 pjsua_var.stun_status = PJ_EPENDING;
1236 status = pjsua_resolve_stun_servers(pjsua_var.ua_cfg.stun_srv_cnt,
1237 pjsua_var.ua_cfg.stun_srv,
1238 wait, NULL,
1239 &internal_stun_resolve_cb);
1240 if (wait || status != PJ_SUCCESS) {
Benny Prijonoda9785b2007-04-02 20:43:06 +00001241 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +00001242 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001243 } else {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001244 pjsua_var.stun_status = PJ_SUCCESS;
1245 }
1246
Benny Prijonoc97608e2007-03-23 16:34:20 +00001247 } else if (pjsua_var.stun_status == PJ_EPENDING) {
1248 /* STUN server resolution has been started, wait for the
1249 * result.
1250 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001251 if (wait) {
1252 while (pjsua_var.stun_status == PJ_EPENDING)
1253 pjsua_handle_events(10);
1254 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001255 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001256
1257 if (pjsua_var.stun_status != PJ_EPENDING &&
1258 pjsua_var.stun_status != PJ_SUCCESS &&
1259 pjsua_var.ua_cfg.stun_ignore_failure)
1260 {
1261 PJ_LOG(2,(THIS_FILE,
1262 "Ignoring STUN resolution failure (by setting)"));
1263 pjsua_var.stun_status = PJ_SUCCESS;
1264 }
1265
1266 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001267}
1268
1269/*
Benny Prijono268ca612006-02-07 12:34:11 +00001270 * Destroy pjsua.
1271 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001272PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001273{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001274 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001275
1276 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001277 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001278
1279 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001280 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1281 if (pjsua_var.thread[i]) {
1282 pj_thread_join(pjsua_var.thread[i]);
1283 pj_thread_destroy(pjsua_var.thread[i]);
1284 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001285 }
Benny Prijono268ca612006-02-07 12:34:11 +00001286 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001287
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001288 if (pjsua_var.endpt) {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001289 unsigned max_wait;
1290
Benny Prijono384dab42009-10-14 01:58:04 +00001291 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1292
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001293 /* Terminate all calls. */
1294 pjsua_call_hangup_all();
1295
Benny Prijono7f6ee022008-07-31 08:32:46 +00001296 /* Set all accounts to offline */
1297 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1298 if (!pjsua_var.acc[i].valid)
1299 continue;
1300 pjsua_var.acc[i].online_status = PJ_FALSE;
1301 pj_bzero(&pjsua_var.acc[i].rpid, sizeof(pjrpid_element));
1302 }
1303
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001304 /* Terminate all presence subscriptions. */
1305 pjsua_pres_shutdown();
1306
Benny Prijono384dab42009-10-14 01:58:04 +00001307 /* Destroy media (to shutdown media transports etc) */
1308 pjsua_media_subsys_destroy();
1309
Benny Prijono534a9ba2009-10-13 14:01:59 +00001310 /* Wait for sometime until all publish client sessions are done
1311 * (ticket #364)
1312 */
1313 /* First stage, get the maximum wait time */
1314 max_wait = 100;
Nanang Izzuddind1f054e2010-08-06 07:18:08 +00001315 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001316 if (!pjsua_var.acc[i].valid)
1317 continue;
1318 if (pjsua_var.acc[i].cfg.unpublish_max_wait_time_msec > max_wait)
1319 max_wait = pjsua_var.acc[i].cfg.unpublish_max_wait_time_msec;
1320 }
1321
1322 /* Second stage, wait for unpublications to complete */
1323 for (i=0; i<(int)(max_wait/50); ++i) {
1324 unsigned j;
1325 for (j=0; j<PJ_ARRAY_SIZE(pjsua_var.acc); ++j) {
1326 if (!pjsua_var.acc[j].valid)
1327 continue;
1328
1329 if (pjsua_var.acc[j].publish_sess)
1330 break;
1331 }
1332 if (j != PJ_ARRAY_SIZE(pjsua_var.acc))
1333 busy_sleep(50);
1334 else
1335 break;
1336 }
1337
1338 /* Third stage, forcefully destroy unfinished unpublications */
Nanang Izzuddind1f054e2010-08-06 07:18:08 +00001339 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001340 if (pjsua_var.acc[i].publish_sess) {
1341 pjsip_publishc_destroy(pjsua_var.acc[i].publish_sess);
1342 pjsua_var.acc[i].publish_sess = NULL;
1343 }
1344 }
1345
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001346 /* Unregister all accounts */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001347 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1348 if (!pjsua_var.acc[i].valid)
1349 continue;
1350
1351 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001352 pjsua_acc_set_registration(i, PJ_FALSE);
1353 }
1354 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001355
1356 /* Terminate any pending STUN resolution */
1357 if (!pj_list_empty(&pjsua_var.stun_res)) {
1358 pjsua_stun_resolve *sess = pjsua_var.stun_res.next;
1359 while (sess != &pjsua_var.stun_res) {
1360 pjsua_stun_resolve *next = sess->next;
1361 destroy_stun_resolve(sess);
1362 sess = next;
1363 }
1364 }
1365
Benny Prijono384dab42009-10-14 01:58:04 +00001366 /* Wait until all unregistrations are done (ticket #364) */
1367 /* First stage, get the maximum wait time */
1368 max_wait = 100;
Nanang Izzuddind1f054e2010-08-06 07:18:08 +00001369 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
Benny Prijono384dab42009-10-14 01:58:04 +00001370 if (!pjsua_var.acc[i].valid)
1371 continue;
1372 if (pjsua_var.acc[i].cfg.unreg_timeout > max_wait)
1373 max_wait = pjsua_var.acc[i].cfg.unreg_timeout;
1374 }
1375
1376 /* Second stage, wait for unregistrations to complete */
1377 for (i=0; i<(int)(max_wait/50); ++i) {
1378 unsigned j;
1379 for (j=0; j<PJ_ARRAY_SIZE(pjsua_var.acc); ++j) {
1380 if (!pjsua_var.acc[j].valid)
1381 continue;
1382
1383 if (pjsua_var.acc[j].regc)
1384 break;
1385 }
1386 if (j != PJ_ARRAY_SIZE(pjsua_var.acc))
1387 busy_sleep(50);
1388 else
1389 break;
1390 }
1391 /* Note variable 'i' is used below */
1392
Benny Prijonoff3b1462008-07-14 09:32:14 +00001393 /* Wait for some time to allow unregistration and ICE/TURN
1394 * transports shutdown to complete:
Benny Prijono384dab42009-10-14 01:58:04 +00001395 */
1396 if (i < 20)
1397 busy_sleep(1000 - i*50);
Benny Prijonoff3b1462008-07-14 09:32:14 +00001398
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001399 PJ_LOG(4,(THIS_FILE, "Destroying..."));
1400
Benny Prijonod7e26582008-07-18 23:51:49 +00001401 /* Must destroy endpoint first before destroying pools in
1402 * buddies or accounts, since shutting down transaction layer
1403 * may emit events which trigger some buddy or account callbacks
1404 * to be called.
1405 */
1406 pjsip_endpt_destroy(pjsua_var.endpt);
1407 pjsua_var.endpt = NULL;
1408
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001409 /* Destroy pool in the buddy object */
1410 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1411 if (pjsua_var.buddy[i].pool) {
1412 pj_pool_release(pjsua_var.buddy[i].pool);
1413 pjsua_var.buddy[i].pool = NULL;
1414 }
1415 }
1416
1417 /* Destroy accounts */
1418 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1419 if (pjsua_var.acc[i].pool) {
1420 pj_pool_release(pjsua_var.acc[i].pool);
1421 pjsua_var.acc[i].pool = NULL;
1422 }
1423 }
Benny Prijono9fc735d2006-05-28 14:58:12 +00001424 }
Benny Prijono268ca612006-02-07 12:34:11 +00001425
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001426 /* Destroy mutex */
1427 if (pjsua_var.mutex) {
1428 pj_mutex_destroy(pjsua_var.mutex);
1429 pjsua_var.mutex = NULL;
1430 }
1431
1432 /* Destroy pool and pool factory. */
1433 if (pjsua_var.pool) {
1434 pj_pool_release(pjsua_var.pool);
1435 pjsua_var.pool = NULL;
1436 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001437
1438 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1439
1440 /* End logging */
1441 if (pjsua_var.log_file) {
1442 pj_file_close(pjsua_var.log_file);
1443 pjsua_var.log_file = NULL;
1444 }
1445
1446 /* Shutdown PJLIB */
1447 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001448 }
Benny Prijono268ca612006-02-07 12:34:11 +00001449
Benny Prijonof762ee72006-12-01 11:14:37 +00001450 /* Clear pjsua_var */
1451 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1452
Benny Prijono268ca612006-02-07 12:34:11 +00001453 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001454 return PJ_SUCCESS;
1455}
1456
1457
1458/**
1459 * Application is recommended to call this function after all initialization
1460 * is done, so that the library can do additional checking set up
1461 * additional
1462 *
1463 * @return PJ_SUCCESS on success, or the appropriate error code.
1464 */
1465PJ_DEF(pj_status_t) pjsua_start(void)
1466{
1467 pj_status_t status;
1468
1469 status = pjsua_call_subsys_start();
1470 if (status != PJ_SUCCESS)
1471 return status;
1472
1473 status = pjsua_media_subsys_start();
1474 if (status != PJ_SUCCESS)
1475 return status;
1476
1477 status = pjsua_pres_start();
1478 if (status != PJ_SUCCESS)
1479 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001480
1481 return PJ_SUCCESS;
1482}
1483
Benny Prijono9fc735d2006-05-28 14:58:12 +00001484
1485/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001486 * Poll pjsua for events, and if necessary block the caller thread for
1487 * the specified maximum interval (in miliseconds).
1488 */
1489PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1490{
Benny Prijono897f9f82007-05-03 19:56:21 +00001491#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001492
1493 return pj_symbianos_poll(-1, msec_timeout);
1494
Benny Prijono897f9f82007-05-03 19:56:21 +00001495#else
1496
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001497 unsigned count = 0;
1498 pj_time_val tv;
1499 pj_status_t status;
1500
1501 tv.sec = 0;
1502 tv.msec = msec_timeout;
1503 pj_time_val_normalize(&tv);
1504
1505 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1506
1507 if (status != PJ_SUCCESS)
1508 return -status;
1509
1510 return count;
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001511
Benny Prijono897f9f82007-05-03 19:56:21 +00001512#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001513}
1514
1515
1516/*
1517 * Create memory pool.
1518 */
1519PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1520 pj_size_t increment)
1521{
1522 /* Pool factory is thread safe, no need to lock */
1523 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1524 NULL);
1525}
1526
1527
1528/*
1529 * Internal function to get SIP endpoint instance of pjsua, which is
1530 * needed for example to register module, create transports, etc.
1531 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001532 */
1533PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1534{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001535 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001536}
1537
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001538/*
1539 * Internal function to get media endpoint instance.
1540 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001541 */
1542PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1543{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001544 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001545}
1546
Benny Prijono97b87172006-08-24 14:25:14 +00001547/*
1548 * Internal function to get PJSUA pool factory.
1549 */
1550PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1551{
1552 return &pjsua_var.cp.factory;
1553}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001554
1555/*****************************************************************************
1556 * PJSUA SIP Transport API.
1557 */
1558
1559/*
Benny Prijono23674a32007-12-01 08:59:25 +00001560 * Tools to get address string.
1561 */
1562static const char *addr_string(const pj_sockaddr_t *addr)
1563{
1564 static char str[128];
1565 str[0] = '\0';
1566 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1567 pj_sockaddr_get_addr(addr),
1568 str, sizeof(str));
1569 return str;
1570}
1571
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001572void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
1573 pjsip_transport_state state,
1574 const pjsip_transport_state_info *info);
1575
Benny Prijono21407322010-03-10 13:33:25 +00001576/* Callback to receive transport state notifications */
1577static void on_tp_state_callback(pjsip_transport *tp,
1578 pjsip_transport_state state,
1579 const pjsip_transport_state_info *info)
1580{
1581 if (pjsua_var.ua_cfg.cb.on_transport_state) {
1582 (*pjsua_var.ua_cfg.cb.on_transport_state)(tp, state, info);
1583 }
1584 if (pjsua_var.old_tp_cb) {
1585 (*pjsua_var.old_tp_cb)(tp, state, info);
1586 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001587 pjsua_acc_on_tp_state_changed(tp, state, info);
Benny Prijono21407322010-03-10 13:33:25 +00001588}
1589
Benny Prijono23674a32007-12-01 08:59:25 +00001590/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001591 * Create and initialize SIP socket (and possibly resolve public
1592 * address via STUN, depending on config).
1593 */
Benny Prijono23674a32007-12-01 08:59:25 +00001594static pj_status_t create_sip_udp_sock(int af,
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001595 const pjsua_transport_config *cfg,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001596 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001597 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001598{
Benny Prijono23674a32007-12-01 08:59:25 +00001599 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001600 unsigned port = cfg->port;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001601 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001602 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001603 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001604 pj_status_t status;
1605
Benny Prijonoc97608e2007-03-23 16:34:20 +00001606 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001607 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001608 if (status != PJ_SUCCESS) {
1609 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1610 return status;
1611 }
1612
Benny Prijono23674a32007-12-01 08:59:25 +00001613 /* Initialize bound address */
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001614 if (cfg->bound_addr.slen) {
1615 status = pj_sockaddr_init(af, &bind_addr, &cfg->bound_addr,
Benny Prijono23674a32007-12-01 08:59:25 +00001616 (pj_uint16_t)port);
1617 if (status != PJ_SUCCESS) {
1618 pjsua_perror(THIS_FILE,
1619 "Unable to resolve transport bound address",
1620 status);
1621 return status;
1622 }
1623 } else {
1624 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1625 }
1626
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001627 /* Create socket */
Benny Prijono23674a32007-12-01 08:59:25 +00001628 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001629 if (status != PJ_SUCCESS) {
1630 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001631 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001632 }
1633
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001634 /* Apply QoS, if specified */
1635 status = pj_sock_apply_qos2(sock, cfg->qos_type,
1636 &cfg->qos_params,
1637 2, THIS_FILE, "SIP UDP socket");
1638
1639 /* Bind socket */
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001640 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001641 if (status != PJ_SUCCESS) {
1642 pjsua_perror(THIS_FILE, "bind() error", status);
1643 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001644 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001645 }
1646
Benny Prijonoe347cb02007-02-14 14:36:13 +00001647 /* If port is zero, get the bound port */
1648 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001649 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001650 int namelen = sizeof(bound_addr);
1651 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1652 if (status != PJ_SUCCESS) {
1653 pjsua_perror(THIS_FILE, "getsockname() error", status);
1654 pj_sock_close(sock);
1655 return status;
1656 }
1657
Benny Prijono23674a32007-12-01 08:59:25 +00001658 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001659 }
1660
Benny Prijonoc97608e2007-03-23 16:34:20 +00001661 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001662 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1663 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001664 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001665 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001666 }
1667
1668 /* Get the published address, either by STUN or by resolving
1669 * the name of local host.
1670 */
Benny Prijono23674a32007-12-01 08:59:25 +00001671 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001672 /*
1673 * Public address is already specified, no need to resolve the
1674 * address, only set the port.
1675 */
Benny Prijono23674a32007-12-01 08:59:25 +00001676 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1677 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001678
1679 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001680 /*
1681 * STUN is specified, resolve the address with STUN.
1682 */
Benny Prijono23674a32007-12-01 08:59:25 +00001683 if (af != pj_AF_INET()) {
1684 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1685 pj_sock_close(sock);
1686 return PJ_EAFNOTSUP;
1687 }
1688
Benny Prijono14c2b862007-02-21 00:40:05 +00001689 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001690 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1691 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001692 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001693 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001694 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001695 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001696 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001697 }
1698
1699 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001700 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001701
Benny Prijono42d08d22007-12-20 11:23:07 +00001702 if (pj_sockaddr_has_addr(&bind_addr)) {
1703 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1704 } else {
1705 status = pj_gethostip(af, p_pub_addr);
1706 if (status != PJ_SUCCESS) {
1707 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1708 pj_sock_close(sock);
1709 return status;
1710 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001711 }
1712
Benny Prijono23674a32007-12-01 08:59:25 +00001713 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1714 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001715 }
1716
1717 *p_sock = sock;
1718
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001719 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001720 addr_string(p_pub_addr),
1721 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001722
Benny Prijonod8410532006-06-15 11:04:33 +00001723 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724}
1725
1726
1727/*
1728 * Create SIP transport.
1729 */
1730PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1731 const pjsua_transport_config *cfg,
1732 pjsua_transport_id *p_id)
1733{
1734 pjsip_transport *tp;
1735 unsigned id;
1736 pj_status_t status;
1737
1738 PJSUA_LOCK();
1739
1740 /* Find empty transport slot */
1741 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001742 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001743 break;
1744 }
1745
1746 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1747 status = PJ_ETOOMANY;
1748 pjsua_perror(THIS_FILE, "Error creating transport", status);
1749 goto on_return;
1750 }
1751
1752 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001753 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001754 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001755 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001756 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001757 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001758 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001759 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001760 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001761 pjsip_host_port addr_name;
1762
1763 /* Supply default config if it's not specified */
1764 if (cfg == NULL) {
1765 pjsua_transport_config_default(&config);
1766 cfg = &config;
1767 }
1768
Benny Prijono0a5cad82006-09-26 13:21:02 +00001769 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001770 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1771 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001772 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001773 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1774 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001775 if (status != PJ_SUCCESS) {
1776 pjsua_perror(THIS_FILE,
1777 "Unable to resolve transport public address",
1778 status);
1779 goto on_return;
1780 }
1781 }
1782
1783 /* Create the socket and possibly resolve the address with STUN
1784 * (only when public address is not specified).
1785 */
Benny Prijono23674a32007-12-01 08:59:25 +00001786 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001787 cfg, &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001788 if (status != PJ_SUCCESS)
1789 goto on_return;
1790
Benny Prijono23674a32007-12-01 08:59:25 +00001791 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1792 addr_name.host = pj_str(hostbuf);
1793 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001794
1795 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001796 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1797 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001798 if (status != PJ_SUCCESS) {
1799 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1800 status);
1801 pj_sock_close(sock);
1802 goto on_return;
1803 }
1804
Benny Prijonoe93e2872006-06-28 16:46:49 +00001805
1806 /* Save the transport */
1807 pjsua_var.tpdata[id].type = type;
1808 pjsua_var.tpdata[id].local_name = tp->local_name;
1809 pjsua_var.tpdata[id].data.tp = tp;
1810
Benny Prijono3569c0d2007-04-06 10:29:20 +00001811#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1812
Benny Prijonob2477142007-12-05 04:09:59 +00001813 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001814 /*
1815 * Create TCP transport.
1816 */
1817 pjsua_transport_config config;
1818 pjsip_tpfactory *tcp;
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001819 pjsip_tcp_transport_cfg tcp_cfg;
1820
1821 pjsip_tcp_transport_cfg_default(&tcp_cfg, pj_AF_INET());
Benny Prijonoe93e2872006-06-28 16:46:49 +00001822
1823 /* Supply default config if it's not specified */
1824 if (cfg == NULL) {
1825 pjsua_transport_config_default(&config);
1826 cfg = &config;
1827 }
1828
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001829 /* Configure bind address */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001830 if (cfg->port)
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001831 pj_sockaddr_set_port(&tcp_cfg.bind_addr, (pj_uint16_t)cfg->port);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001832
Benny Prijono0a5cad82006-09-26 13:21:02 +00001833 if (cfg->bound_addr.slen) {
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001834 status = pj_sockaddr_set_str_addr(tcp_cfg.af,
1835 &tcp_cfg.bind_addr,
1836 &cfg->bound_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001837 if (status != PJ_SUCCESS) {
1838 pjsua_perror(THIS_FILE,
1839 "Unable to resolve transport bound address",
1840 status);
1841 goto on_return;
1842 }
1843 }
1844
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001845 /* Set published name */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001846 if (cfg->public_addr.slen)
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001847 tcp_cfg.addr_name.host = cfg->public_addr;
1848
1849 /* Copy the QoS settings */
1850 tcp_cfg.qos_type = cfg->qos_type;
1851 pj_memcpy(&tcp_cfg.qos_params, &cfg->qos_params,
1852 sizeof(cfg->qos_params));
Benny Prijonoe93e2872006-06-28 16:46:49 +00001853
1854 /* Create the TCP transport */
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001855 status = pjsip_tcp_transport_start3(pjsua_var.endpt, &tcp_cfg, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001856
1857 if (status != PJ_SUCCESS) {
1858 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1859 status);
1860 goto on_return;
1861 }
1862
1863 /* Save the transport */
1864 pjsua_var.tpdata[id].type = type;
1865 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1866 pjsua_var.tpdata[id].data.factory = tcp;
1867
Benny Prijono3569c0d2007-04-06 10:29:20 +00001868#endif /* PJ_HAS_TCP */
1869
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001870#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1871 } else if (type == PJSIP_TRANSPORT_TLS) {
1872 /*
1873 * Create TLS transport.
1874 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001875 pjsua_transport_config config;
1876 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001877 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001878 pj_sockaddr_in local_addr;
1879
1880 /* Supply default config if it's not specified */
1881 if (cfg == NULL) {
1882 pjsua_transport_config_default(&config);
1883 config.port = 5061;
1884 cfg = &config;
1885 }
1886
1887 /* Init local address */
1888 pj_sockaddr_in_init(&local_addr, 0, 0);
1889
1890 if (cfg->port)
1891 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1892
1893 if (cfg->bound_addr.slen) {
1894 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1895 if (status != PJ_SUCCESS) {
1896 pjsua_perror(THIS_FILE,
1897 "Unable to resolve transport bound address",
1898 status);
1899 goto on_return;
1900 }
1901 }
1902
1903 /* Init published name */
1904 pj_bzero(&a_name, sizeof(pjsip_host_port));
1905 if (cfg->public_addr.slen)
1906 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001907
1908 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001909 &cfg->tls_setting,
1910 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001911 if (status != PJ_SUCCESS) {
1912 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1913 status);
1914 goto on_return;
1915 }
1916
1917 /* Save the transport */
1918 pjsua_var.tpdata[id].type = type;
1919 pjsua_var.tpdata[id].local_name = tls->addr_name;
1920 pjsua_var.tpdata[id].data.factory = tls;
1921#endif
1922
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001923 } else {
1924 status = PJSIP_EUNSUPTRANSPORT;
1925 pjsua_perror(THIS_FILE, "Error creating transport", status);
1926 goto on_return;
1927 }
1928
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +00001929 /* Set transport state callback */
1930 if (pjsua_var.ua_cfg.cb.on_transport_state) {
Benny Prijono21407322010-03-10 13:33:25 +00001931 pjsip_tp_state_callback tpcb;
1932 pjsip_tpmgr *tpmgr;
1933
1934 tpmgr = pjsip_endpt_get_tpmgr(pjsua_var.endpt);
Nanang Izzuddin91cfa962010-04-14 06:57:35 +00001935 tpcb = pjsip_tpmgr_get_state_cb(tpmgr);
Benny Prijono21407322010-03-10 13:33:25 +00001936
1937 if (tpcb != &on_tp_state_callback) {
1938 pjsua_var.old_tp_cb = tpcb;
Nanang Izzuddin91cfa962010-04-14 06:57:35 +00001939 pjsip_tpmgr_set_state_cb(tpmgr, &on_tp_state_callback);
Benny Prijono21407322010-03-10 13:33:25 +00001940 }
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +00001941 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001942
1943 /* Return the ID */
1944 if (p_id) *p_id = id;
1945
1946 status = PJ_SUCCESS;
1947
1948on_return:
1949
1950 PJSUA_UNLOCK();
1951
Benny Prijonod8410532006-06-15 11:04:33 +00001952 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001953}
1954
1955
1956/*
1957 * Register transport that has been created by application.
1958 */
1959PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1960 pjsua_transport_id *p_id)
1961{
1962 unsigned id;
1963
1964 PJSUA_LOCK();
1965
1966 /* Find empty transport slot */
1967 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001968 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001969 break;
1970 }
1971
1972 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1973 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1974 PJSUA_UNLOCK();
1975 return PJ_ETOOMANY;
1976 }
1977
1978 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001979 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001980 pjsua_var.tpdata[id].local_name = tp->local_name;
1981 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001982
1983 /* Return the ID */
1984 if (p_id) *p_id = id;
1985
1986 PJSUA_UNLOCK();
1987
1988 return PJ_SUCCESS;
1989}
1990
1991
1992/*
1993 * Enumerate all transports currently created in the system.
1994 */
1995PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1996 unsigned *p_count )
1997{
1998 unsigned i, count;
1999
2000 PJSUA_LOCK();
2001
2002 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
2003 ++i)
2004 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00002005 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002006 continue;
2007
2008 id[count++] = i;
2009 }
2010
2011 *p_count = count;
2012
2013 PJSUA_UNLOCK();
2014
2015 return PJ_SUCCESS;
2016}
2017
2018
2019/*
2020 * Get information about transports.
2021 */
2022PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
2023 pjsua_transport_info *info)
2024{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002025 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00002026 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002027
Benny Prijonoac623b32006-07-03 15:19:31 +00002028 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002029
2030 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002031 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
2032 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002033
2034 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00002035 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002036
2037 PJSUA_LOCK();
2038
Benny Prijonoe93e2872006-06-28 16:46:49 +00002039 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
2040
2041 pjsip_transport *tp = t->data.tp;
2042
2043 if (tp == NULL) {
2044 PJSUA_UNLOCK();
2045 return PJ_EINVALIDOP;
2046 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002047
Benny Prijonoe93e2872006-06-28 16:46:49 +00002048 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00002049 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00002050 info->type_name = pj_str(tp->type_name);
2051 info->info = pj_str(tp->info);
2052 info->flag = tp->flag;
2053 info->addr_len = tp->addr_len;
2054 info->local_addr = tp->local_addr;
2055 info->local_name = tp->local_name;
2056 info->usage_count = pj_atomic_get(tp->ref_cnt);
2057
Benny Prijono0a5cad82006-09-26 13:21:02 +00002058 status = PJ_SUCCESS;
2059
Benny Prijonoe93e2872006-06-28 16:46:49 +00002060 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
2061
2062 pjsip_tpfactory *factory = t->data.factory;
2063
2064 if (factory == NULL) {
2065 PJSUA_UNLOCK();
2066 return PJ_EINVALIDOP;
2067 }
2068
2069 info->id = id;
2070 info->type = t->type;
2071 info->type_name = pj_str("TCP");
2072 info->info = pj_str("TCP transport");
2073 info->flag = factory->flag;
2074 info->addr_len = sizeof(factory->local_addr);
2075 info->local_addr = factory->local_addr;
2076 info->local_name = factory->addr_name;
2077 info->usage_count = 0;
2078
Benny Prijono0a5cad82006-09-26 13:21:02 +00002079 status = PJ_SUCCESS;
2080
2081 } else {
2082 pj_assert(!"Unsupported transport");
2083 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00002084 }
2085
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002086
2087 PJSUA_UNLOCK();
2088
Benny Prijono0a5cad82006-09-26 13:21:02 +00002089 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002090}
2091
2092
2093/*
2094 * Disable a transport or re-enable it.
2095 */
2096PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
2097 pj_bool_t enabled)
2098{
2099 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002100 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
2101 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002102
2103 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00002104 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002105
2106
2107 /* To be done!! */
2108 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002109 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002110
2111 return PJ_EINVALIDOP;
2112}
2113
2114
2115/*
2116 * Close the transport.
2117 */
2118PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
2119 pj_bool_t force )
2120{
Benny Prijono5ff61872007-02-01 03:37:11 +00002121 pj_status_t status;
2122
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002123 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002124 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
2125 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002126
2127 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00002128 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002129
Benny Prijono0a5cad82006-09-26 13:21:02 +00002130 /* Note: destroy() may not work if there are objects still referencing
2131 * the transport.
2132 */
2133 if (force) {
2134 switch (pjsua_var.tpdata[id].type) {
2135 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00002136 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
2137 if (status != PJ_SUCCESS)
2138 return status;
2139 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
2140 if (status != PJ_SUCCESS)
2141 return status;
2142 break;
2143
2144 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00002145 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00002146 /* This will close the TCP listener, but existing TCP/TLS
2147 * connections (if any) will still linger
2148 */
2149 status = (*pjsua_var.tpdata[id].data.factory->destroy)
2150 (pjsua_var.tpdata[id].data.factory);
2151 if (status != PJ_SUCCESS)
2152 return status;
2153
Benny Prijono0a5cad82006-09-26 13:21:02 +00002154 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00002155
Benny Prijono1ebd6142006-10-19 15:48:02 +00002156 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00002157 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00002158 }
2159
2160 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00002161 /* If force is not specified, transports will be closed at their
2162 * convenient time. However this will leak PJSUA-API transport
2163 * descriptors as PJSUA-API wouldn't know when exactly the
2164 * transport is closed thus it can't cleanup PJSUA transport
2165 * descriptor.
2166 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00002167 switch (pjsua_var.tpdata[id].type) {
2168 case PJSIP_TRANSPORT_UDP:
2169 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00002170 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00002171 case PJSIP_TRANSPORT_TCP:
2172 return (*pjsua_var.tpdata[id].data.factory->destroy)
2173 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002174 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00002175 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00002176 }
2177 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002178
Benny Prijono5ff61872007-02-01 03:37:11 +00002179 /* Cleanup pjsua data when force is applied */
2180 if (force) {
2181 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
2182 pjsua_var.tpdata[id].data.ptr = NULL;
2183 }
2184
2185 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002186}
2187
2188
2189/*
2190 * Add additional headers etc in msg_data specified by application
2191 * when sending requests.
2192 */
2193void pjsua_process_msg_data(pjsip_tx_data *tdata,
2194 const pjsua_msg_data *msg_data)
2195{
2196 pj_bool_t allow_body;
2197 const pjsip_hdr *hdr;
2198
Benny Prijono56315612006-07-18 14:39:40 +00002199 /* Always add User-Agent */
2200 if (pjsua_var.ua_cfg.user_agent.slen &&
2201 tdata->msg->type == PJSIP_REQUEST_MSG)
2202 {
2203 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
2204 pjsip_hdr *h;
2205 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
2206 &STR_USER_AGENT,
2207 &pjsua_var.ua_cfg.user_agent);
2208 pjsip_msg_add_hdr(tdata->msg, h);
2209 }
2210
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002211 if (!msg_data)
2212 return;
2213
2214 hdr = msg_data->hdr_list.next;
2215 while (hdr && hdr != &msg_data->hdr_list) {
2216 pjsip_hdr *new_hdr;
2217
Benny Prijonoa1e69682007-05-11 15:14:34 +00002218 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002219 pjsip_msg_add_hdr(tdata->msg, new_hdr);
2220
2221 hdr = hdr->next;
2222 }
2223
2224 allow_body = (tdata->msg->body == NULL);
2225
2226 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
2227 pjsip_media_type ctype;
2228 pjsip_msg_body *body;
2229
2230 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
2231 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
2232 &msg_data->msg_body);
2233 tdata->msg->body = body;
2234 }
Benny Prijono1c1d7342010-08-01 09:48:51 +00002235
2236 /* Multipart */
2237 if (!pj_list_empty(&msg_data->multipart_parts) &&
2238 msg_data->multipart_ctype.type.slen)
2239 {
2240 pjsip_msg_body *bodies;
2241 pjsip_multipart_part *part;
2242 pj_str_t *boundary = NULL;
2243
2244 bodies = pjsip_multipart_create(tdata->pool,
2245 &msg_data->multipart_ctype,
2246 boundary);
2247 part = msg_data->multipart_parts.next;
2248 while (part != &msg_data->multipart_parts) {
2249 pjsip_multipart_part *part_copy;
2250
2251 part_copy = pjsip_multipart_clone_part(tdata->pool, part);
2252 pjsip_multipart_add_part(tdata->pool, bodies, part_copy);
2253 part = part->next;
2254 }
2255
2256 if (tdata->msg->body) {
2257 part = pjsip_multipart_create_part(tdata->pool);
2258 part->body = tdata->msg->body;
2259 pjsip_multipart_add_part(tdata->pool, bodies, part);
2260
2261 tdata->msg->body = NULL;
2262 }
2263
2264 tdata->msg->body = bodies;
2265 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002266}
2267
2268
2269/*
2270 * Add route_set to outgoing requests
2271 */
2272void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
2273 const pjsip_route_hdr *route_set )
2274{
2275 const pjsip_route_hdr *r;
2276
2277 r = route_set->next;
2278 while (r != route_set) {
2279 pjsip_route_hdr *new_r;
2280
Benny Prijonoa1e69682007-05-11 15:14:34 +00002281 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002282 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
2283
2284 r = r->next;
2285 }
2286}
2287
2288
2289/*
2290 * Simple version of MIME type parsing (it doesn't support parameters)
2291 */
2292void pjsua_parse_media_type( pj_pool_t *pool,
2293 const pj_str_t *mime,
2294 pjsip_media_type *media_type)
2295{
2296 pj_str_t tmp;
2297 char *pos;
2298
Benny Prijonoac623b32006-07-03 15:19:31 +00002299 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002300
2301 pj_strdup_with_null(pool, &tmp, mime);
2302
2303 pos = pj_strchr(&tmp, '/');
2304 if (pos) {
2305 media_type->type.ptr = tmp.ptr;
2306 media_type->type.slen = (pos-tmp.ptr);
2307 media_type->subtype.ptr = pos+1;
2308 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
2309 } else {
2310 media_type->type = tmp;
2311 }
2312}
2313
2314
2315/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002316 * Internal function to init transport selector from transport id.
2317 */
2318void pjsua_init_tpselector(pjsua_transport_id tp_id,
2319 pjsip_tpselector *sel)
2320{
2321 pjsua_transport_data *tpdata;
2322 unsigned flag;
2323
2324 pj_bzero(sel, sizeof(*sel));
2325 if (tp_id == PJSUA_INVALID_ID)
2326 return;
2327
Benny Prijonoa1e69682007-05-11 15:14:34 +00002328 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002329 tpdata = &pjsua_var.tpdata[tp_id];
2330
2331 flag = pjsip_transport_get_flag_from_type(tpdata->type);
2332
2333 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
2334 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
2335 sel->u.transport = tpdata->data.tp;
2336 } else {
2337 sel->type = PJSIP_TPSELECTOR_LISTENER;
2338 sel->u.listener = tpdata->data.factory;
2339 }
2340}
2341
2342
Benny Prijono6ba8c542007-10-16 01:34:14 +00002343/* Callback upon NAT detection completion */
2344static void nat_detect_cb(void *user_data,
2345 const pj_stun_nat_detect_result *res)
2346{
2347 PJ_UNUSED_ARG(user_data);
2348
2349 pjsua_var.nat_in_progress = PJ_FALSE;
2350 pjsua_var.nat_status = res->status;
2351 pjsua_var.nat_type = res->nat_type;
2352
2353 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
2354 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
2355 }
2356}
2357
2358
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002359/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002360 * Detect NAT type.
2361 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00002362PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002363{
2364 pj_status_t status;
2365
Benny Prijono6ba8c542007-10-16 01:34:14 +00002366 if (pjsua_var.nat_in_progress)
2367 return PJ_SUCCESS;
2368
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002369 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +00002370 status = resolve_stun_server(PJ_TRUE);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002371 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002372 pjsua_var.nat_status = status;
2373 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002374 return status;
2375 }
2376
2377 /* Make sure we have STUN */
2378 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002379 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
2380 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002381 }
2382
Benny Prijono6ba8c542007-10-16 01:34:14 +00002383 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
2384 &pjsua_var.stun_cfg,
2385 NULL, &nat_detect_cb);
2386
2387 if (status != PJ_SUCCESS) {
2388 pjsua_var.nat_status = status;
2389 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
2390 return status;
2391 }
2392
2393 pjsua_var.nat_in_progress = PJ_TRUE;
2394
2395 return PJ_SUCCESS;
2396}
2397
2398
2399/*
2400 * Get NAT type.
2401 */
2402PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
2403{
2404 *type = pjsua_var.nat_type;
2405 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002406}
2407
Benny Prijonoc7545782010-09-28 07:43:18 +00002408/*
2409 * Verify that valid url is given.
2410 */
2411PJ_DEF(pj_status_t) pjsua_verify_url(const char *c_url)
2412{
2413 pjsip_uri *p;
2414 pj_pool_t *pool;
2415 char *url;
2416 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2417
2418 if (!len) return PJSIP_EINVALIDURI;
2419
2420 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
2421 if (!pool) return PJ_ENOMEM;
2422
2423 url = (char*) pj_pool_alloc(pool, len+1);
2424 pj_ansi_strcpy(url, c_url);
2425
2426 p = pjsip_parse_uri(pool, url, len, 0);
2427
2428 pj_pool_release(pool);
2429 return p ? 0 : PJSIP_EINVALIDURI;
2430}
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002431
2432/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002433 * Verify that valid SIP url is given.
2434 */
2435PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
2436{
2437 pjsip_uri *p;
2438 pj_pool_t *pool;
2439 char *url;
2440 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2441
Benny Prijonoc7545782010-09-28 07:43:18 +00002442 if (!len) return PJSIP_EINVALIDURI;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002443
2444 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
Benny Prijonoc7545782010-09-28 07:43:18 +00002445 if (!pool) return PJ_ENOMEM;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002446
Benny Prijonoa1e69682007-05-11 15:14:34 +00002447 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002448 pj_ansi_strcpy(url, c_url);
2449
2450 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002451 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
2452 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
2453 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002454 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002455 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002456
2457 pj_pool_release(pool);
Benny Prijonoc7545782010-09-28 07:43:18 +00002458 return p ? 0 : PJSIP_EINVALIDURI;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002459}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002460
Benny Prijono73bb7232009-10-20 13:56:26 +00002461/*
2462 * Schedule a timer entry.
2463 */
2464PJ_DEF(pj_status_t) pjsua_schedule_timer( pj_timer_entry *entry,
2465 const pj_time_val *delay)
2466{
2467 return pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, delay);
2468}
2469
2470/*
2471 * Cancel the previously scheduled timer.
2472 *
2473 */
2474PJ_DEF(void) pjsua_cancel_timer(pj_timer_entry *entry)
2475{
2476 pjsip_endpt_cancel_timer(pjsua_var.endpt, entry);
2477}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002478
Benny Prijono91d06b62008-09-20 12:16:56 +00002479/**
2480 * Normalize route URI (check for ";lr" and append one if it doesn't
2481 * exist and pjsua_config.force_lr is set.
2482 */
2483pj_status_t normalize_route_uri(pj_pool_t *pool, pj_str_t *uri)
2484{
2485 pj_str_t tmp_uri;
2486 pj_pool_t *tmp_pool;
2487 pjsip_uri *uri_obj;
2488 pjsip_sip_uri *sip_uri;
2489
2490 tmp_pool = pjsua_pool_create("tmplr%p", 512, 512);
2491 if (!tmp_pool)
2492 return PJ_ENOMEM;
2493
2494 pj_strdup_with_null(tmp_pool, &tmp_uri, uri);
2495
2496 uri_obj = pjsip_parse_uri(tmp_pool, tmp_uri.ptr, tmp_uri.slen, 0);
2497 if (!uri_obj) {
2498 PJ_LOG(1,(THIS_FILE, "Invalid route URI: %.*s",
2499 (int)uri->slen, uri->ptr));
2500 pj_pool_release(tmp_pool);
2501 return PJSIP_EINVALIDURI;
2502 }
2503
2504 if (!PJSIP_URI_SCHEME_IS_SIP(uri_obj) &&
2505 !PJSIP_URI_SCHEME_IS_SIP(uri_obj))
2506 {
2507 PJ_LOG(1,(THIS_FILE, "Route URI must be SIP URI: %.*s",
2508 (int)uri->slen, uri->ptr));
2509 pj_pool_release(tmp_pool);
2510 return PJSIP_EINVALIDSCHEME;
2511 }
2512
2513 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri_obj);
2514
2515 /* Done if force_lr is disabled or if lr parameter is present */
2516 if (!pjsua_var.ua_cfg.force_lr || sip_uri->lr_param) {
2517 pj_pool_release(tmp_pool);
2518 return PJ_SUCCESS;
2519 }
2520
2521 /* Set lr param */
2522 sip_uri->lr_param = 1;
2523
2524 /* Print the URI */
2525 tmp_uri.ptr = (char*) pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE);
2526 tmp_uri.slen = pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, uri_obj,
2527 tmp_uri.ptr, PJSIP_MAX_URL_SIZE);
2528 if (tmp_uri.slen < 1) {
2529 PJ_LOG(1,(THIS_FILE, "Route URI is too long: %.*s",
2530 (int)uri->slen, uri->ptr));
2531 pj_pool_release(tmp_pool);
2532 return PJSIP_EURITOOLONG;
2533 }
2534
2535 /* Clone the URI */
2536 pj_strdup_with_null(pool, uri, &tmp_uri);
2537
Benny Prijonodc20c592009-10-15 06:55:30 +00002538 pj_pool_release(tmp_pool);
Benny Prijono91d06b62008-09-20 12:16:56 +00002539 return PJ_SUCCESS;
2540}
2541
Benny Prijonoda9785b2007-04-02 20:43:06 +00002542/*
2543 * This is a utility function to dump the stack states to log, using
2544 * verbosity level 3.
2545 */
2546PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2547{
2548 unsigned old_decor;
2549 unsigned i;
Benny Prijonoda9785b2007-04-02 20:43:06 +00002550
2551 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2552
2553 old_decor = pj_log_get_decor();
2554 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2555
2556 if (detail)
2557 pj_dump_config();
2558
2559 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2560
2561 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2562
2563 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2564 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2565 pjsua_call *call = &pjsua_var.calls[i];
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002566 pjmedia_transport_info tpinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002567 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002568
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002569 /* MSVC complains about tpinfo not being initialized */
Benny Prijono734fc2d2008-03-17 16:05:35 +00002570 //pj_bzero(&tpinfo, sizeof(tpinfo));
Benny Prijono4f093d22007-04-09 08:54:32 +00002571
Benny Prijono734fc2d2008-03-17 16:05:35 +00002572 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002573 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002574
Benny Prijono5186eae2007-12-03 14:38:25 +00002575 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002576 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002577 pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, addr_buf,
Benny Prijono5186eae2007-12-03 14:38:25 +00002578 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002579 }
2580
2581 pjsip_tsx_layer_dump(detail);
2582 pjsip_ua_dump(detail);
2583
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002584// Dumping complete call states may require a 'large' buffer
2585// (about 3KB per call session, including RTCP XR).
2586#if 0
Benny Prijonoda9785b2007-04-02 20:43:06 +00002587 /* Dump all invite sessions: */
2588 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2589
2590 if (pjsua_call_get_count() == 0) {
2591
2592 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2593
2594 } else {
2595 unsigned i;
2596
2597 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2598 if (pjsua_call_is_active(i)) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002599 /* Tricky logging, since call states log string tends to be
2600 * longer than PJ_LOG_MAX_SIZE.
2601 */
2602 char buf[1024 * 3];
2603 unsigned call_dump_len;
2604 unsigned part_len;
2605 unsigned part_idx;
2606 unsigned log_decor;
2607
Benny Prijonoda9785b2007-04-02 20:43:06 +00002608 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002609 call_dump_len = strlen(buf);
2610
2611 log_decor = pj_log_get_decor();
2612 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE |
2613 PJ_LOG_HAS_CR));
2614 PJ_LOG(3,(THIS_FILE, "\n"));
2615 pj_log_set_decor(0);
2616
2617 part_idx = 0;
2618 part_len = PJ_LOG_MAX_SIZE-80;
2619 while (part_idx < call_dump_len) {
2620 char p_orig, *p;
2621
2622 p = &buf[part_idx];
2623 if (part_idx + part_len > call_dump_len)
2624 part_len = call_dump_len - part_idx;
2625 p_orig = p[part_len];
2626 p[part_len] = '\0';
2627 PJ_LOG(3,(THIS_FILE, "%s", p));
2628 p[part_len] = p_orig;
2629 part_idx += part_len;
2630 }
2631 pj_log_set_decor(log_decor);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002632 }
2633 }
2634 }
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002635#endif
Benny Prijonoda9785b2007-04-02 20:43:06 +00002636
2637 /* Dump presence status */
2638 pjsua_pres_dump(detail);
2639
2640 pj_log_set_decor(old_decor);
2641 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2642}
2643