blob: fd8c57f5ce34c2efbbfcc64c6eac0fe51d2af04a [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 Prijonob54719f2010-11-16 03:07:46 +0000186 cfg->use_rfc5626 = PJ_TRUE;
Benny Prijono29c8ca32010-06-22 06:02:13 +0000187 cfg->reg_use_proxy = PJSUA_REG_USE_OUTBOUND_PROXY |
188 PJSUA_REG_USE_ACC_PROXY;
Nanang Izzuddin5e39a2b2010-09-20 06:13:02 +0000189#if defined(PJMEDIA_STREAM_ENABLE_KA) && PJMEDIA_STREAM_ENABLE_KA!=0
190 cfg->use_stream_ka = (PJMEDIA_STREAM_ENABLE_KA != 0);
191#endif
Nanang Izzuddin60e8aa92010-09-28 10:48:48 +0000192 pj_list_init(&cfg->reg_hdr_list);
Benny Prijonodd63b992010-10-01 02:03:42 +0000193 cfg->call_hold_type = PJSUA_CALL_HOLD_TYPE_DEFAULT;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000194}
195
196PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
197{
198 pj_bzero(cfg, sizeof(*cfg));
199}
200
201PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
202{
203 pj_bzero(cfg, sizeof(*cfg));
204
205 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono50f19b32008-03-11 13:15:43 +0000206 cfg->snd_clock_rate = 0;
Benny Prijono7d60d052008-03-29 12:24:20 +0000207 cfg->channel_count = 1;
Benny Prijono37c710b2008-01-10 12:09:26 +0000208 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
209 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000210 cfg->has_ioqueue = PJ_TRUE;
211 cfg->thread_cnt = 1;
212 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
213 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
214 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
Benny Prijono10454dc2009-02-21 14:21:59 +0000215 cfg->snd_rec_latency = PJMEDIA_SND_DEFAULT_REC_LATENCY;
216 cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000217 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
Benny Prijonof798e502009-03-09 13:08:16 +0000218 cfg->snd_auto_close_time = 1;
Benny Prijonof76e1392008-06-06 14:51:48 +0000219
Benny Prijono329d6382009-05-29 13:04:03 +0000220 cfg->ice_max_host_cands = -1;
221 pj_ice_sess_options_default(&cfg->ice_opt);
222
Benny Prijonof76e1392008-06-06 14:51:48 +0000223 cfg->turn_conn_type = PJ_TURN_TP_UDP;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000224}
225
Benny Prijonodc39fe82006-05-26 12:17:46 +0000226
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000227/*****************************************************************************
228 * This is a very simple PJSIP module, whose sole purpose is to display
229 * incoming and outgoing messages to log. This module will have priority
230 * higher than transport layer, which means:
231 *
232 * - incoming messages will come to this module first before reaching
233 * transaction layer.
234 *
235 * - outgoing messages will come to this module last, after the message
236 * has been 'printed' to contiguous buffer by transport layer and
237 * appropriate transport instance has been decided for this message.
238 *
239 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000240
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000241/* Notification on incoming messages */
242static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
243{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000244 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
245 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000246 "--end msg--",
247 rdata->msg_info.len,
248 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000249 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000250 rdata->pkt_info.src_name,
251 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000252 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000253 rdata->msg_info.msg_buf));
254
255 /* Always return false, otherwise messages will not get processed! */
256 return PJ_FALSE;
257}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000258
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000259/* Notification on outgoing messages */
260static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
261{
262
263 /* Important note:
264 * tp_info field is only valid after outgoing messages has passed
265 * transport layer. So don't try to access tp_info when the module
266 * has lower priority than transport layer.
267 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000268
Benny Prijonob4a17c92006-07-10 14:40:21 +0000269 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
270 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000271 "--end msg--",
272 (tdata->buf.cur - tdata->buf.start),
273 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000274 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000275 tdata->tp_info.dst_name,
276 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000277 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000278 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000279
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000280 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000281 return PJ_SUCCESS;
282}
283
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000284/* The module instance. */
285static pjsip_module pjsua_msg_logger =
286{
287 NULL, NULL, /* prev, next. */
288 { "mod-pjsua-log", 13 }, /* Name. */
289 -1, /* Id */
290 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
291 NULL, /* load() */
292 NULL, /* start() */
293 NULL, /* stop() */
294 NULL, /* unload() */
295 &logging_on_rx_msg, /* on_rx_request() */
296 &logging_on_rx_msg, /* on_rx_response() */
297 &logging_on_tx_msg, /* on_tx_request. */
298 &logging_on_tx_msg, /* on_tx_response() */
299 NULL, /* on_tsx_state() */
300
301};
302
303
304/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000305 * Another simple module to handle incoming OPTIONS request
306 */
307
308/* Notification on incoming request */
309static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
310{
311 pjsip_tx_data *tdata;
312 pjsip_response_addr res_addr;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000313 pjmedia_transport_info tpinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000314 pjmedia_sdp_session *sdp;
315 const pjsip_hdr *cap_hdr;
316 pj_status_t status;
317
318 /* Only want to handle OPTIONS requests */
319 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000320 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000321 {
322 return PJ_FALSE;
323 }
324
Benny Prijono384dab42009-10-14 01:58:04 +0000325 /* Don't want to handle if shutdown is in progress */
326 if (pjsua_var.thread_quit_flag) {
327 pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata,
328 PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL,
329 NULL, NULL);
330 return PJ_TRUE;
331 }
332
Benny Prijono56315612006-07-18 14:39:40 +0000333 /* Create basic response. */
334 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
335 &tdata);
336 if (status != PJ_SUCCESS) {
337 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
338 return PJ_TRUE;
339 }
340
341 /* Add Allow header */
342 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
343 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000344 pjsip_msg_add_hdr(tdata->msg,
345 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000346 }
347
348 /* Add Accept header */
349 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
350 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000351 pjsip_msg_add_hdr(tdata->msg,
352 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000353 }
354
355 /* Add Supported header */
356 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
357 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000358 pjsip_msg_add_hdr(tdata->msg,
359 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000360 }
361
362 /* Add Allow-Events header from the evsub module */
363 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
364 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000365 pjsip_msg_add_hdr(tdata->msg,
366 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000367 }
368
369 /* Add User-Agent header */
370 if (pjsua_var.ua_cfg.user_agent.slen) {
371 const pj_str_t USER_AGENT = { "User-Agent", 10};
372 pjsip_hdr *h;
373
374 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
375 &USER_AGENT,
376 &pjsua_var.ua_cfg.user_agent);
377 pjsip_msg_add_hdr(tdata->msg, h);
378 }
379
Benny Prijonoa248b952009-08-12 22:28:47 +0000380 /* Get media socket info, make sure transport is ready */
381 if (pjsua_var.calls[0].med_tp) {
382 pjmedia_transport_info_init(&tpinfo);
383 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000384
Benny Prijonoa248b952009-08-12 22:28:47 +0000385 /* Add SDP body, using call0's RTP address */
386 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
387 &tpinfo.sock_info, &sdp);
388 if (status == PJ_SUCCESS) {
389 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
390 }
Benny Prijono56315612006-07-18 14:39:40 +0000391 }
392
393 /* Send response statelessly */
394 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
395 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
396 if (status != PJ_SUCCESS)
397 pjsip_tx_data_dec_ref(tdata);
398
399 return PJ_TRUE;
400}
401
402
403/* The module instance. */
404static pjsip_module pjsua_options_handler =
405{
406 NULL, NULL, /* prev, next. */
407 { "mod-pjsua-options", 17 }, /* Name. */
408 -1, /* Id */
409 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
410 NULL, /* load() */
411 NULL, /* start() */
412 NULL, /* stop() */
413 NULL, /* unload() */
414 &options_on_rx_request, /* on_rx_request() */
415 NULL, /* on_rx_response() */
416 NULL, /* on_tx_request. */
417 NULL, /* on_tx_response() */
418 NULL, /* on_tsx_state() */
419
420};
421
422
423/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424 * These two functions are the main callbacks registered to PJSIP stack
425 * to receive SIP request and response messages that are outside any
426 * dialogs and any transactions.
427 */
Benny Prijono268ca612006-02-07 12:34:11 +0000428
429/*
430 * Handler for receiving incoming requests.
431 *
432 * This handler serves multiple purposes:
433 * - it receives requests outside dialogs.
434 * - it receives requests inside dialogs, when the requests are
435 * unhandled by other dialog usages. Example of these
436 * requests are: MESSAGE.
437 */
438static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
439{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440 pj_bool_t processed = PJ_FALSE;
441
442 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000443
Benny Prijono84126ab2006-02-09 09:30:09 +0000444 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000445
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000446 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000447 }
448
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449 PJSUA_UNLOCK();
450
451 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000452}
453
454
455/*
456 * Handler for receiving incoming responses.
457 *
458 * This handler serves multiple purposes:
459 * - it receives strayed responses (i.e. outside any dialog and
460 * outside any transactions).
461 * - it receives responses coming to a transaction, when pjsua
462 * module is set as transaction user for the transaction.
463 * - it receives responses inside a dialog, when these responses
464 * are unhandled by other dialog usages.
465 */
466static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
467{
468 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000469 return PJ_FALSE;
470}
471
472
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000473/*****************************************************************************
474 * Logging.
475 */
476
477/* Log callback */
478static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000479{
Benny Prijono572d4852006-11-23 21:50:02 +0000480 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000481
482 if (pjsua_var.log_file) {
483 pj_ssize_t size = len;
484 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000485 /* This will slow things down considerably! Don't do it!
486 pj_file_flush(pjsua_var.log_file);
487 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000488 }
489
Benny Prijono572d4852006-11-23 21:50:02 +0000490 if (level <= (int)pjsua_var.log_cfg.console_level) {
491 if (pjsua_var.log_cfg.cb)
492 (*pjsua_var.log_cfg.cb)(level, buffer, len);
493 else
494 pj_log_write(level, buffer, len);
495 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000496}
497
498
499/*
500 * Application can call this function at any time (after pjsua_create(), of
501 * course) to change logging settings.
502 */
503PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
504{
505 pj_status_t status;
506
507 /* Save config. */
508 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
509
510 /* Redirect log function to ours */
511 pj_log_set_log_func( &log_writer );
512
Benny Prijono9cb09a22007-08-30 09:35:10 +0000513 /* Set decor */
514 pj_log_set_decor(pjsua_var.log_cfg.decor);
515
Benny Prijono4190cf92008-01-18 13:25:05 +0000516 /* Set log level */
517 pj_log_set_level(pjsua_var.log_cfg.level);
518
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 /* Close existing file, if any */
520 if (pjsua_var.log_file) {
521 pj_file_close(pjsua_var.log_file);
522 pjsua_var.log_file = NULL;
523 }
524
525 /* If output log file is desired, create the file: */
526 if (pjsua_var.log_cfg.log_filename.slen) {
Benny Prijonodbe3f4b2009-05-07 16:56:04 +0000527 unsigned flags = PJ_O_WRONLY;
528 flags |= pjsua_var.log_cfg.log_file_flags;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 status = pj_file_open(pjsua_var.pool,
530 pjsua_var.log_cfg.log_filename.ptr,
Benny Prijonodbe3f4b2009-05-07 16:56:04 +0000531 flags,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000532 &pjsua_var.log_file);
533
534 if (status != PJ_SUCCESS) {
535 pjsua_perror(THIS_FILE, "Error creating log file", status);
536 return status;
537 }
538 }
539
540 /* Unregister msg logging if it's previously registered */
541 if (pjsua_msg_logger.id >= 0) {
542 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
543 pjsua_msg_logger.id = -1;
544 }
545
546 /* Enable SIP message logging */
547 if (pjsua_var.log_cfg.msg_logging)
548 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
549
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000550 return PJ_SUCCESS;
551}
552
553
554/*****************************************************************************
555 * PJSUA Base API.
556 */
557
558/* Worker thread function. */
559static int worker_thread(void *arg)
560{
561 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000562
Benny Prijono268ca612006-02-07 12:34:11 +0000563 PJ_UNUSED_ARG(arg);
564
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000565 while (!pjsua_var.thread_quit_flag) {
566 int count;
567
568 count = pjsua_handle_events(TIMEOUT);
569 if (count < 0)
570 pj_thread_sleep(TIMEOUT);
571 }
Benny Prijono268ca612006-02-07 12:34:11 +0000572
573 return 0;
574}
575
Benny Prijonodc39fe82006-05-26 12:17:46 +0000576
Benny Prijono8389c312008-02-21 21:36:34 +0000577/* Init random seed */
578static void init_random_seed(void)
579{
580 pj_sockaddr addr;
581 const pj_str_t *hostname;
582 pj_uint32_t pid;
583 pj_time_val t;
584 unsigned seed=0;
585
586 /* Add hostname */
587 hostname = pj_gethostname();
588 seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen);
589
590 /* Add primary IP address */
591 if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS)
592 seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4);
593
594 /* Get timeofday */
595 pj_gettimeofday(&t);
596 seed = pj_hash_calc(seed, &t, sizeof(t));
597
598 /* Add PID */
599 pid = pj_getpid();
600 seed = pj_hash_calc(seed, &pid, sizeof(pid));
601
602 /* Init random seed */
603 pj_srand(seed);
604}
605
Benny Prijono268ca612006-02-07 12:34:11 +0000606/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000607 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000608 */
609PJ_DEF(pj_status_t) pjsua_create(void)
610{
611 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000612
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613 /* Init pjsua data */
614 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000615
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000616 /* Set default logging settings */
617 pjsua_logging_config_default(&pjsua_var.log_cfg);
618
619 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000620 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000621 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
622
Benny Prijono8389c312008-02-21 21:36:34 +0000623 /* Init random seed */
624 init_random_seed();
Benny Prijono268ca612006-02-07 12:34:11 +0000625
Benny Prijonofccab712006-02-22 22:23:22 +0000626 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000627 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000628 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000629
Benny Prijonoec921342007-03-24 13:00:30 +0000630 /* Init PJNATH */
631 status = pjnath_init();
632 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000633
Benny Prijono094d3ad2006-11-21 08:41:00 +0000634 /* Set default sound device ID */
Benny Prijono96e74f32009-02-22 12:00:12 +0000635 pjsua_var.cap_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
636 pjsua_var.play_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
Benny Prijono094d3ad2006-11-21 08:41:00 +0000637
Benny Prijono268ca612006-02-07 12:34:11 +0000638 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000639 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000640
641 /* Create memory pool for application. */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000642 pjsua_var.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000643
644 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000645
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000646 /* Create mutex */
647 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
648 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000649 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000650 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000651 return status;
652 }
653
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000654 /* Must create SIP endpoint to initialize SIP parser. The parser
655 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000656 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000657 status = pjsip_endpt_create(&pjsua_var.cp.factory,
658 pj_gethostname()->ptr,
659 &pjsua_var.endpt);
660 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000661
662
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000663 return PJ_SUCCESS;
664}
665
666
667/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000668 * Initialize pjsua with the specified settings. All the settings are
669 * optional, and the default values will be used when the config is not
670 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000671 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000672PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
673 const pjsua_logging_config *log_cfg,
674 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000675{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000676 pjsua_config default_cfg;
677 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000678 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000679 pjsip_ua_init_param ua_init_param;
Benny Prijono29c8ca32010-06-22 06:02:13 +0000680 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000681 pj_status_t status;
682
683
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000684 /* Create default configurations when the config is not supplied */
685
686 if (ua_cfg == NULL) {
687 pjsua_config_default(&default_cfg);
688 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000689 }
690
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000691 if (media_cfg == NULL) {
692 pjsua_media_config_default(&default_media_cfg);
693 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000694 }
695
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000696 /* Initialize logging first so that info/errors can be captured */
697 if (log_cfg) {
698 status = pjsua_reconfigure_logging(log_cfg);
Benny Prijono329d6382009-05-29 13:04:03 +0000699 if (status != PJ_SUCCESS)
700 return status;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000701 }
702
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000703 /* If nameserver is configured, create DNS resolver instance and
704 * set it to be used by SIP resolver.
705 */
706 if (ua_cfg->nameserver_count) {
707#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000708 unsigned i;
709
710 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000711 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
712 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000713 if (status != PJ_SUCCESS) {
714 pjsua_perror(THIS_FILE, "Error creating resolver", status);
715 return status;
716 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000717
718 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000719 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
720 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000721 ua_cfg->nameserver, NULL);
722 if (status != PJ_SUCCESS) {
723 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
724 return status;
725 }
726
727 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000728 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000729 if (status != PJ_SUCCESS) {
730 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
731 return status;
732 }
733
734 /* Print nameservers */
735 for (i=0; i<ua_cfg->nameserver_count; ++i) {
736 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
737 (int)ua_cfg->nameserver[i].slen,
738 ua_cfg->nameserver[i].ptr));
739 }
740#else
741 PJ_LOG(2,(THIS_FILE,
742 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
743#endif
744 }
745
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000746 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000747
748 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000749 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
750 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000751
Benny Prijonodc39fe82006-05-26 12:17:46 +0000752
753 /* Initialize UA layer module: */
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000754 pj_bzero(&ua_init_param, sizeof(ua_init_param));
755 if (ua_cfg->hangup_forked_call) {
756 ua_init_param.on_dlg_forked = &on_dlg_forked;
757 }
758 status = pjsip_ua_init_module( pjsua_var.endpt, &ua_init_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000759 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000760
Benny Prijonodc39fe82006-05-26 12:17:46 +0000761
Benny Prijono053f5222006-11-11 16:16:04 +0000762 /* Initialize Replaces support. */
763 status = pjsip_replaces_init_module( pjsua_var.endpt );
764 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
765
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000766 /* Initialize 100rel support */
767 status = pjsip_100rel_init_module(pjsua_var.endpt);
768 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000769
Nanang Izzuddin59dffb12009-08-11 12:42:38 +0000770 /* Initialize session timer support */
771 status = pjsip_timer_init_module(pjsua_var.endpt);
772 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
773
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000774 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000775 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000776 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000777 {
778 NULL, NULL, /* prev, next. */
779 { "mod-pjsua", 9 }, /* Name. */
780 -1, /* Id */
781 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
782 NULL, /* load() */
783 NULL, /* start() */
784 NULL, /* stop() */
785 NULL, /* unload() */
786 &mod_pjsua_on_rx_request, /* on_rx_request() */
787 &mod_pjsua_on_rx_response, /* on_rx_response() */
788 NULL, /* on_tx_request. */
789 NULL, /* on_tx_response() */
790 NULL, /* on_tsx_state() */
791 };
792
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000793 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000794
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000795 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
796 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000797 }
798
Benny Prijono29c8ca32010-06-22 06:02:13 +0000799 /* Parse outbound proxies */
800 for (i=0; i<ua_cfg->outbound_proxy_cnt; ++i) {
801 pj_str_t tmp;
802 pj_str_t hname = { "Route", 5};
803 pjsip_route_hdr *r;
804
805 pj_strdup_with_null(pjsua_var.pool, &tmp, &ua_cfg->outbound_proxy[i]);
806
807 r = (pjsip_route_hdr*)
808 pjsip_parse_hdr(pjsua_var.pool, &hname, tmp.ptr,
809 (unsigned)tmp.slen, NULL);
810 if (r == NULL) {
811 pjsua_perror(THIS_FILE, "Invalid outbound proxy URI",
812 PJSIP_EINVALIDURI);
813 return PJSIP_EINVALIDURI;
814 }
815
816 if (pjsua_var.ua_cfg.force_lr) {
817 pjsip_sip_uri *sip_url;
818 if (!PJSIP_URI_SCHEME_IS_SIP(r->name_addr.uri) &&
819 !PJSIP_URI_SCHEME_IS_SIP(r->name_addr.uri))
820 {
821 return PJSIP_EINVALIDSCHEME;
822 }
823 sip_url = (pjsip_sip_uri*)r->name_addr.uri;
824 sip_url->lr_param = 1;
825 }
826
827 pj_list_push_back(&pjsua_var.outbound_proxy, r);
828 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000829
Benny Prijonodc39fe82006-05-26 12:17:46 +0000830
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000831 /* Initialize PJSUA call subsystem: */
832 status = pjsua_call_subsys_init(ua_cfg);
833 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000834 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000835
Benny Prijonobb995fd2009-08-12 11:03:23 +0000836 /* Convert deprecated STUN settings */
837 if (pjsua_var.ua_cfg.stun_srv_cnt==0) {
838 if (pjsua_var.ua_cfg.stun_domain.slen) {
839 pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] =
840 pjsua_var.ua_cfg.stun_domain;
841 }
842 if (pjsua_var.ua_cfg.stun_host.slen) {
843 pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] =
844 pjsua_var.ua_cfg.stun_host;
845 }
846 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000847
Benny Prijonoc97608e2007-03-23 16:34:20 +0000848 /* Start resolving STUN server */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000849 status = resolve_stun_server(PJ_FALSE);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000850 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
851 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
852 return status;
853 }
854
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000855 /* Initialize PJSUA media subsystem */
856 status = pjsua_media_subsys_init(media_cfg);
857 if (status != PJ_SUCCESS)
858 goto on_error;
859
Benny Prijonodc39fe82006-05-26 12:17:46 +0000860
861 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000862 status = pjsip_evsub_init_module(pjsua_var.endpt);
863 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000864
Benny Prijonodc39fe82006-05-26 12:17:46 +0000865
866 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000867 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
868 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000869
Benny Prijono4dd961b2009-10-26 11:21:37 +0000870 /* Initialize MWI support */
871 status = pjsip_mwi_init_module(pjsua_var.endpt, pjsip_evsub_instance());
872
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000873 /* Init PUBLISH module */
874 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000875
876 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000877 status = pjsip_xfer_init_module( pjsua_var.endpt );
878 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000879
880 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000881 status = pjsua_pres_init();
882 if (status != PJ_SUCCESS)
883 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000884
885 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000886 status = pjsua_im_init();
887 if (status != PJ_SUCCESS)
888 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000889
Benny Prijonoc8141a82006-08-20 09:12:19 +0000890 /* Register OPTIONS handler */
891 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
892
893 /* Add OPTIONS in Allow header */
894 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
895 NULL, 1, &STR_OPTIONS);
896
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000897 /* Start worker thread if needed. */
898 if (pjsua_var.ua_cfg.thread_cnt) {
899 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000900
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000901 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
902 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000903
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000904 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
905 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
906 NULL, 0, 0, &pjsua_var.thread[i]);
907 if (status != PJ_SUCCESS)
908 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000909 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000910 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
911 pjsua_var.ua_cfg.thread_cnt));
912 } else {
913 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000914 }
915
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000916 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000917
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000918 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000919 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000920
Benny Prijono268ca612006-02-07 12:34:11 +0000921 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000922
923on_error:
924 pjsua_destroy();
925 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000926}
927
928
Benny Prijono834aee32006-02-19 01:38:06 +0000929/* Sleep with polling */
930static void busy_sleep(unsigned msec)
931{
932 pj_time_val timeout, now;
933
934 pj_gettimeofday(&timeout);
935 timeout.msec += msec;
936 pj_time_val_normalize(&timeout);
937
938 do {
Nanang Izzuddin90b83202009-03-02 15:48:45 +0000939 int i;
940 i = msec / 10;
941 while (pjsua_handle_events(10) > 0 && i > 0)
942 --i;
Benny Prijono834aee32006-02-19 01:38:06 +0000943 pj_gettimeofday(&now);
944 } while (PJ_TIME_VAL_LT(now, timeout));
945}
946
Benny Prijonobb995fd2009-08-12 11:03:23 +0000947/* Internal function to destroy STUN resolution session
948 * (pj_stun_resolve).
Benny Prijonoebbf6892007-03-24 17:37:25 +0000949 */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000950static void destroy_stun_resolve(pjsua_stun_resolve *sess)
Benny Prijonoebbf6892007-03-24 17:37:25 +0000951{
Benny Prijonobb995fd2009-08-12 11:03:23 +0000952 PJSUA_LOCK();
953 pj_list_erase(sess);
954 PJSUA_UNLOCK();
Benny Prijonoebbf6892007-03-24 17:37:25 +0000955
Benny Prijonobb995fd2009-08-12 11:03:23 +0000956 pj_assert(sess->stun_sock==NULL);
957 pj_pool_release(sess->pool);
958}
Benny Prijonoebbf6892007-03-24 17:37:25 +0000959
Benny Prijonobb995fd2009-08-12 11:03:23 +0000960/* This is the internal function to be called when STUN resolution
961 * session (pj_stun_resolve) has completed.
962 */
963static void stun_resolve_complete(pjsua_stun_resolve *sess)
964{
965 pj_stun_resolve_result result;
966
967 pj_bzero(&result, sizeof(result));
968 result.token = sess->token;
969 result.status = sess->status;
970 result.name = sess->srv[sess->idx];
971 pj_memcpy(&result.addr, &sess->addr, sizeof(result.addr));
972
973 if (result.status == PJ_SUCCESS) {
974 char addr[PJ_INET6_ADDRSTRLEN+10];
975 pj_sockaddr_print(&result.addr, addr, sizeof(addr), 3);
976 PJ_LOG(4,(THIS_FILE,
977 "STUN resolution success, using %.*s, address is %s",
978 (int)sess->srv[sess->idx].slen,
979 sess->srv[sess->idx].ptr,
980 addr));
981 } else {
982 char errmsg[PJ_ERR_MSG_SIZE];
983 pj_strerror(result.status, errmsg, sizeof(errmsg));
984 PJ_LOG(1,(THIS_FILE, "STUN resolution failed: %s", errmsg));
985 }
986
987 sess->cb(&result);
988
989 if (!sess->blocking) {
990 destroy_stun_resolve(sess);
991 }
992}
993
994/* This is the callback called by the STUN socket (pj_stun_sock)
995 * to report it's state. We use this as part of testing the
996 * STUN server.
997 */
998static pj_bool_t test_stun_on_status(pj_stun_sock *stun_sock,
999 pj_stun_sock_op op,
1000 pj_status_t status)
1001{
1002 pjsua_stun_resolve *sess;
1003
Nanang Izzuddinae1c6152009-08-17 16:30:04 +00001004 sess = (pjsua_stun_resolve*) pj_stun_sock_get_user_data(stun_sock);
Benny Prijonobb995fd2009-08-12 11:03:23 +00001005 pj_assert(stun_sock == sess->stun_sock);
1006
Benny Prijonoebbf6892007-03-24 17:37:25 +00001007 if (status != PJ_SUCCESS) {
Benny Prijonobb995fd2009-08-12 11:03:23 +00001008 char errmsg[PJ_ERR_MSG_SIZE];
1009 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonoebbf6892007-03-24 17:37:25 +00001010
Benny Prijonobb995fd2009-08-12 11:03:23 +00001011 PJ_LOG(4,(THIS_FILE, "STUN resolution for %.*s failed: %s",
1012 (int)sess->srv[sess->idx].slen,
1013 sess->srv[sess->idx].ptr, errmsg));
Benny Prijonoebbf6892007-03-24 17:37:25 +00001014
Benny Prijonobb995fd2009-08-12 11:03:23 +00001015 sess->status = status;
1016
1017 pj_stun_sock_destroy(stun_sock);
1018 sess->stun_sock = NULL;
1019
1020 ++sess->idx;
1021 resolve_stun_entry(sess);
1022
1023 return PJ_FALSE;
1024
1025 } else if (op == PJ_STUN_SOCK_BINDING_OP) {
1026 pj_stun_sock_info ssi;
1027
1028 pj_stun_sock_get_info(stun_sock, &ssi);
1029 pj_memcpy(&sess->addr, &ssi.srv_addr, sizeof(sess->addr));
1030
1031 sess->status = PJ_SUCCESS;
1032 pj_stun_sock_destroy(stun_sock);
1033 sess->stun_sock = NULL;
1034
1035 stun_resolve_complete(sess);
1036
1037 return PJ_FALSE;
1038
1039 } else
1040 return PJ_TRUE;
1041
1042}
1043
1044/* This is an internal function to resolve and test current
1045 * server entry in pj_stun_resolve session. It is called by
1046 * pjsua_resolve_stun_servers() and test_stun_on_status() above
1047 */
1048static void resolve_stun_entry(pjsua_stun_resolve *sess)
1049{
1050 /* Loop while we have entry to try */
1051 for (; sess->idx < sess->count; ++sess->idx) {
1052 const int af = pj_AF_INET();
1053 pj_str_t hostpart;
1054 pj_uint16_t port;
1055 pj_stun_sock_cb stun_sock_cb;
1056
1057 pj_assert(sess->idx < sess->count);
1058
1059 /* Parse the server entry into host:port */
1060 sess->status = pj_sockaddr_parse2(af, 0, &sess->srv[sess->idx],
1061 &hostpart, &port, NULL);
1062 if (sess->status != PJ_SUCCESS) {
1063 PJ_LOG(2,(THIS_FILE, "Invalid STUN server entry %.*s",
1064 (int)sess->srv[sess->idx].slen,
1065 sess->srv[sess->idx].ptr));
1066 continue;
Benny Prijonoebbf6892007-03-24 17:37:25 +00001067 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001068
1069 /* Use default port if not specified */
1070 if (port == 0)
1071 port = PJ_STUN_PORT;
1072
1073 pj_assert(sess->stun_sock == NULL);
1074
1075 PJ_LOG(4,(THIS_FILE, "Trying STUN server %.*s (%d of %d)..",
1076 (int)sess->srv[sess->idx].slen,
1077 sess->srv[sess->idx].ptr,
1078 sess->idx+1, sess->count));
1079
1080 /* Use STUN_sock to test this entry */
1081 pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb));
1082 stun_sock_cb.on_status = &test_stun_on_status;
1083 sess->status = pj_stun_sock_create(&pjsua_var.stun_cfg, "stunresolve",
1084 pj_AF_INET(), &stun_sock_cb,
1085 NULL, sess, &sess->stun_sock);
1086 if (sess->status != PJ_SUCCESS) {
1087 char errmsg[PJ_ERR_MSG_SIZE];
1088 pj_strerror(sess->status, errmsg, sizeof(errmsg));
1089 PJ_LOG(4,(THIS_FILE,
1090 "Error creating STUN socket for %.*s: %s",
1091 (int)sess->srv[sess->idx].slen,
1092 sess->srv[sess->idx].ptr, errmsg));
1093
1094 continue;
1095 }
1096
1097 sess->status = pj_stun_sock_start(sess->stun_sock, &hostpart,
1098 port, pjsua_var.resolver);
1099 if (sess->status != PJ_SUCCESS) {
1100 char errmsg[PJ_ERR_MSG_SIZE];
1101 pj_strerror(sess->status, errmsg, sizeof(errmsg));
1102 PJ_LOG(4,(THIS_FILE,
1103 "Error starting STUN socket for %.*s: %s",
1104 (int)sess->srv[sess->idx].slen,
1105 sess->srv[sess->idx].ptr, errmsg));
1106
1107 pj_stun_sock_destroy(sess->stun_sock);
1108 sess->stun_sock = NULL;
1109 continue;
1110 }
1111
1112 /* Done for now, testing will resume/complete asynchronously in
1113 * stun_sock_cb()
1114 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001115 return;
1116 }
1117
Benny Prijonobb995fd2009-08-12 11:03:23 +00001118 if (sess->idx >= sess->count) {
1119 /* No more entries to try */
1120 PJ_ASSERT_ON_FAIL(sess->status != PJ_SUCCESS,
1121 sess->status = PJ_EUNKNOWN);
1122 stun_resolve_complete(sess);
1123 }
1124}
Benny Prijonoebbf6892007-03-24 17:37:25 +00001125
Benny Prijonoebbf6892007-03-24 17:37:25 +00001126
Benny Prijonobb995fd2009-08-12 11:03:23 +00001127/*
1128 * Resolve STUN server.
1129 */
1130PJ_DEF(pj_status_t) pjsua_resolve_stun_servers( unsigned count,
1131 pj_str_t srv[],
1132 pj_bool_t wait,
1133 void *token,
1134 pj_stun_resolve_cb cb)
1135{
1136 pj_pool_t *pool;
1137 pjsua_stun_resolve *sess;
1138 pj_status_t status;
1139 unsigned i;
1140
1141 PJ_ASSERT_RETURN(count && srv && cb, PJ_EINVAL);
1142
1143 pool = pjsua_pool_create("stunres", 256, 256);
1144 if (!pool)
1145 return PJ_ENOMEM;
1146
1147 sess = PJ_POOL_ZALLOC_T(pool, pjsua_stun_resolve);
1148 sess->pool = pool;
1149 sess->token = token;
1150 sess->cb = cb;
1151 sess->count = count;
1152 sess->blocking = wait;
1153 sess->status = PJ_EPENDING;
1154 sess->srv = (pj_str_t*) pj_pool_calloc(pool, count, sizeof(pj_str_t));
1155 for (i=0; i<count; ++i) {
1156 pj_strdup(pool, &sess->srv[i], &srv[i]);
Benny Prijonoebbf6892007-03-24 17:37:25 +00001157 }
1158
Benny Prijonobb995fd2009-08-12 11:03:23 +00001159 PJSUA_LOCK();
1160 pj_list_push_back(&pjsua_var.stun_res, sess);
1161 PJSUA_UNLOCK();
1162
1163 resolve_stun_entry(sess);
1164
1165 if (!wait)
1166 return PJ_SUCCESS;
1167
1168 while (sess->status == PJ_EPENDING) {
1169 pjsua_handle_events(50);
1170 }
1171
1172 status = sess->status;
1173 destroy_stun_resolve(sess);
1174
1175 return status;
1176}
1177
1178/*
1179 * Cancel pending STUN resolution.
1180 */
1181PJ_DEF(pj_status_t) pjsua_cancel_stun_resolution( void *token,
1182 pj_bool_t notify_cb)
1183{
1184 pjsua_stun_resolve *sess;
1185 unsigned cancelled_count = 0;
1186
1187 PJSUA_LOCK();
1188 sess = pjsua_var.stun_res.next;
1189 while (sess != &pjsua_var.stun_res) {
1190 pjsua_stun_resolve *next = sess->next;
1191
1192 if (sess->token == token) {
1193 if (notify_cb) {
1194 pj_stun_resolve_result result;
1195
1196 pj_bzero(&result, sizeof(result));
1197 result.token = token;
1198 result.status = PJ_ECANCELLED;
1199
1200 sess->cb(&result);
1201 }
1202
1203 destroy_stun_resolve(sess);
1204 ++cancelled_count;
1205 }
1206
1207 sess = next;
1208 }
1209 PJSUA_UNLOCK();
1210
1211 return cancelled_count ? PJ_SUCCESS : PJ_ENOTFOUND;
1212}
1213
1214static void internal_stun_resolve_cb(const pj_stun_resolve_result *result)
1215{
1216 pjsua_var.stun_status = result->status;
1217 if (result->status == PJ_SUCCESS) {
1218 pj_memcpy(&pjsua_var.stun_srv, &result->addr, sizeof(result->addr));
1219 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001220}
1221
Benny Prijono268ca612006-02-07 12:34:11 +00001222/*
Benny Prijonoc97608e2007-03-23 16:34:20 +00001223 * Resolve STUN server.
1224 */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001225pj_status_t resolve_stun_server(pj_bool_t wait)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001226{
1227 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
Benny Prijonobb995fd2009-08-12 11:03:23 +00001228 pj_status_t status;
1229
Benny Prijonoc97608e2007-03-23 16:34:20 +00001230 /* Initialize STUN configuration */
1231 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
1232 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
1233 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
1234
1235 /* Start STUN server resolution */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001236 if (pjsua_var.ua_cfg.stun_srv_cnt) {
1237 pjsua_var.stun_status = PJ_EPENDING;
1238 status = pjsua_resolve_stun_servers(pjsua_var.ua_cfg.stun_srv_cnt,
1239 pjsua_var.ua_cfg.stun_srv,
1240 wait, NULL,
1241 &internal_stun_resolve_cb);
1242 if (wait || status != PJ_SUCCESS) {
Benny Prijonoda9785b2007-04-02 20:43:06 +00001243 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +00001244 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001245 } else {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001246 pjsua_var.stun_status = PJ_SUCCESS;
1247 }
1248
Benny Prijonoc97608e2007-03-23 16:34:20 +00001249 } else if (pjsua_var.stun_status == PJ_EPENDING) {
1250 /* STUN server resolution has been started, wait for the
1251 * result.
1252 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001253 if (wait) {
1254 while (pjsua_var.stun_status == PJ_EPENDING)
1255 pjsua_handle_events(10);
1256 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001257 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001258
1259 if (pjsua_var.stun_status != PJ_EPENDING &&
1260 pjsua_var.stun_status != PJ_SUCCESS &&
1261 pjsua_var.ua_cfg.stun_ignore_failure)
1262 {
1263 PJ_LOG(2,(THIS_FILE,
1264 "Ignoring STUN resolution failure (by setting)"));
1265 pjsua_var.stun_status = PJ_SUCCESS;
1266 }
1267
1268 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001269}
1270
1271/*
Benny Prijono268ca612006-02-07 12:34:11 +00001272 * Destroy pjsua.
1273 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001274PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001275{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001276 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001277
1278 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001279 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001280
1281 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001282 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1283 if (pjsua_var.thread[i]) {
1284 pj_thread_join(pjsua_var.thread[i]);
1285 pj_thread_destroy(pjsua_var.thread[i]);
1286 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001287 }
Benny Prijono268ca612006-02-07 12:34:11 +00001288 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001289
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001290 if (pjsua_var.endpt) {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001291 unsigned max_wait;
1292
Benny Prijono384dab42009-10-14 01:58:04 +00001293 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1294
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001295 /* Terminate all calls. */
1296 pjsua_call_hangup_all();
1297
Benny Prijono7f6ee022008-07-31 08:32:46 +00001298 /* Set all accounts to offline */
1299 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1300 if (!pjsua_var.acc[i].valid)
1301 continue;
1302 pjsua_var.acc[i].online_status = PJ_FALSE;
1303 pj_bzero(&pjsua_var.acc[i].rpid, sizeof(pjrpid_element));
1304 }
1305
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001306 /* Terminate all presence subscriptions. */
1307 pjsua_pres_shutdown();
1308
Benny Prijono384dab42009-10-14 01:58:04 +00001309 /* Destroy media (to shutdown media transports etc) */
1310 pjsua_media_subsys_destroy();
1311
Benny Prijono534a9ba2009-10-13 14:01:59 +00001312 /* Wait for sometime until all publish client sessions are done
1313 * (ticket #364)
1314 */
1315 /* First stage, get the maximum wait time */
1316 max_wait = 100;
Nanang Izzuddind1f054e2010-08-06 07:18:08 +00001317 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001318 if (!pjsua_var.acc[i].valid)
1319 continue;
1320 if (pjsua_var.acc[i].cfg.unpublish_max_wait_time_msec > max_wait)
1321 max_wait = pjsua_var.acc[i].cfg.unpublish_max_wait_time_msec;
1322 }
1323
1324 /* Second stage, wait for unpublications to complete */
1325 for (i=0; i<(int)(max_wait/50); ++i) {
1326 unsigned j;
1327 for (j=0; j<PJ_ARRAY_SIZE(pjsua_var.acc); ++j) {
1328 if (!pjsua_var.acc[j].valid)
1329 continue;
1330
1331 if (pjsua_var.acc[j].publish_sess)
1332 break;
1333 }
1334 if (j != PJ_ARRAY_SIZE(pjsua_var.acc))
1335 busy_sleep(50);
1336 else
1337 break;
1338 }
1339
1340 /* Third stage, forcefully destroy unfinished unpublications */
Nanang Izzuddind1f054e2010-08-06 07:18:08 +00001341 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
Benny Prijono534a9ba2009-10-13 14:01:59 +00001342 if (pjsua_var.acc[i].publish_sess) {
1343 pjsip_publishc_destroy(pjsua_var.acc[i].publish_sess);
1344 pjsua_var.acc[i].publish_sess = NULL;
1345 }
1346 }
1347
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001348 /* Unregister all accounts */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001349 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1350 if (!pjsua_var.acc[i].valid)
1351 continue;
1352
1353 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001354 pjsua_acc_set_registration(i, PJ_FALSE);
1355 }
1356 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001357
1358 /* Terminate any pending STUN resolution */
1359 if (!pj_list_empty(&pjsua_var.stun_res)) {
1360 pjsua_stun_resolve *sess = pjsua_var.stun_res.next;
1361 while (sess != &pjsua_var.stun_res) {
1362 pjsua_stun_resolve *next = sess->next;
1363 destroy_stun_resolve(sess);
1364 sess = next;
1365 }
1366 }
1367
Benny Prijono384dab42009-10-14 01:58:04 +00001368 /* Wait until all unregistrations are done (ticket #364) */
1369 /* First stage, get the maximum wait time */
1370 max_wait = 100;
Nanang Izzuddind1f054e2010-08-06 07:18:08 +00001371 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
Benny Prijono384dab42009-10-14 01:58:04 +00001372 if (!pjsua_var.acc[i].valid)
1373 continue;
1374 if (pjsua_var.acc[i].cfg.unreg_timeout > max_wait)
1375 max_wait = pjsua_var.acc[i].cfg.unreg_timeout;
1376 }
1377
1378 /* Second stage, wait for unregistrations to complete */
1379 for (i=0; i<(int)(max_wait/50); ++i) {
1380 unsigned j;
1381 for (j=0; j<PJ_ARRAY_SIZE(pjsua_var.acc); ++j) {
1382 if (!pjsua_var.acc[j].valid)
1383 continue;
1384
1385 if (pjsua_var.acc[j].regc)
1386 break;
1387 }
1388 if (j != PJ_ARRAY_SIZE(pjsua_var.acc))
1389 busy_sleep(50);
1390 else
1391 break;
1392 }
1393 /* Note variable 'i' is used below */
1394
Benny Prijonoff3b1462008-07-14 09:32:14 +00001395 /* Wait for some time to allow unregistration and ICE/TURN
1396 * transports shutdown to complete:
Benny Prijono384dab42009-10-14 01:58:04 +00001397 */
1398 if (i < 20)
1399 busy_sleep(1000 - i*50);
Benny Prijonoff3b1462008-07-14 09:32:14 +00001400
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001401 PJ_LOG(4,(THIS_FILE, "Destroying..."));
1402
Benny Prijonod7e26582008-07-18 23:51:49 +00001403 /* Must destroy endpoint first before destroying pools in
1404 * buddies or accounts, since shutting down transaction layer
1405 * may emit events which trigger some buddy or account callbacks
1406 * to be called.
1407 */
1408 pjsip_endpt_destroy(pjsua_var.endpt);
1409 pjsua_var.endpt = NULL;
1410
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001411 /* Destroy pool in the buddy object */
1412 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1413 if (pjsua_var.buddy[i].pool) {
1414 pj_pool_release(pjsua_var.buddy[i].pool);
1415 pjsua_var.buddy[i].pool = NULL;
1416 }
1417 }
1418
1419 /* Destroy accounts */
1420 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1421 if (pjsua_var.acc[i].pool) {
1422 pj_pool_release(pjsua_var.acc[i].pool);
1423 pjsua_var.acc[i].pool = NULL;
1424 }
1425 }
Benny Prijono9fc735d2006-05-28 14:58:12 +00001426 }
Benny Prijono268ca612006-02-07 12:34:11 +00001427
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001428 /* Destroy mutex */
1429 if (pjsua_var.mutex) {
1430 pj_mutex_destroy(pjsua_var.mutex);
1431 pjsua_var.mutex = NULL;
1432 }
1433
1434 /* Destroy pool and pool factory. */
1435 if (pjsua_var.pool) {
1436 pj_pool_release(pjsua_var.pool);
1437 pjsua_var.pool = NULL;
1438 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001439
1440 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1441
1442 /* End logging */
1443 if (pjsua_var.log_file) {
1444 pj_file_close(pjsua_var.log_file);
1445 pjsua_var.log_file = NULL;
1446 }
1447
1448 /* Shutdown PJLIB */
1449 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001450 }
Benny Prijono268ca612006-02-07 12:34:11 +00001451
Benny Prijonof762ee72006-12-01 11:14:37 +00001452 /* Clear pjsua_var */
1453 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1454
Benny Prijono268ca612006-02-07 12:34:11 +00001455 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001456 return PJ_SUCCESS;
1457}
1458
1459
1460/**
1461 * Application is recommended to call this function after all initialization
1462 * is done, so that the library can do additional checking set up
1463 * additional
1464 *
1465 * @return PJ_SUCCESS on success, or the appropriate error code.
1466 */
1467PJ_DEF(pj_status_t) pjsua_start(void)
1468{
1469 pj_status_t status;
1470
1471 status = pjsua_call_subsys_start();
1472 if (status != PJ_SUCCESS)
1473 return status;
1474
1475 status = pjsua_media_subsys_start();
1476 if (status != PJ_SUCCESS)
1477 return status;
1478
1479 status = pjsua_pres_start();
1480 if (status != PJ_SUCCESS)
1481 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001482
1483 return PJ_SUCCESS;
1484}
1485
Benny Prijono9fc735d2006-05-28 14:58:12 +00001486
1487/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001488 * Poll pjsua for events, and if necessary block the caller thread for
1489 * the specified maximum interval (in miliseconds).
1490 */
1491PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1492{
Benny Prijono897f9f82007-05-03 19:56:21 +00001493#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001494
1495 return pj_symbianos_poll(-1, msec_timeout);
1496
Benny Prijono897f9f82007-05-03 19:56:21 +00001497#else
1498
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001499 unsigned count = 0;
1500 pj_time_val tv;
1501 pj_status_t status;
1502
1503 tv.sec = 0;
1504 tv.msec = msec_timeout;
1505 pj_time_val_normalize(&tv);
1506
1507 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1508
1509 if (status != PJ_SUCCESS)
1510 return -status;
1511
1512 return count;
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001513
Benny Prijono897f9f82007-05-03 19:56:21 +00001514#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001515}
1516
1517
1518/*
1519 * Create memory pool.
1520 */
1521PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1522 pj_size_t increment)
1523{
1524 /* Pool factory is thread safe, no need to lock */
1525 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1526 NULL);
1527}
1528
1529
1530/*
1531 * Internal function to get SIP endpoint instance of pjsua, which is
1532 * needed for example to register module, create transports, etc.
1533 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001534 */
1535PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1536{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001537 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001538}
1539
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001540/*
1541 * Internal function to get media endpoint instance.
1542 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001543 */
1544PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1545{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001546 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001547}
1548
Benny Prijono97b87172006-08-24 14:25:14 +00001549/*
1550 * Internal function to get PJSUA pool factory.
1551 */
1552PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1553{
1554 return &pjsua_var.cp.factory;
1555}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001556
1557/*****************************************************************************
1558 * PJSUA SIP Transport API.
1559 */
1560
1561/*
Benny Prijono23674a32007-12-01 08:59:25 +00001562 * Tools to get address string.
1563 */
1564static const char *addr_string(const pj_sockaddr_t *addr)
1565{
1566 static char str[128];
1567 str[0] = '\0';
1568 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1569 pj_sockaddr_get_addr(addr),
1570 str, sizeof(str));
1571 return str;
1572}
1573
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001574void pjsua_acc_on_tp_state_changed(pjsip_transport *tp,
1575 pjsip_transport_state state,
1576 const pjsip_transport_state_info *info);
1577
Benny Prijono21407322010-03-10 13:33:25 +00001578/* Callback to receive transport state notifications */
1579static void on_tp_state_callback(pjsip_transport *tp,
1580 pjsip_transport_state state,
1581 const pjsip_transport_state_info *info)
1582{
1583 if (pjsua_var.ua_cfg.cb.on_transport_state) {
1584 (*pjsua_var.ua_cfg.cb.on_transport_state)(tp, state, info);
1585 }
1586 if (pjsua_var.old_tp_cb) {
1587 (*pjsua_var.old_tp_cb)(tp, state, info);
1588 }
Nanang Izzuddin36dd5b62010-03-30 11:13:59 +00001589 pjsua_acc_on_tp_state_changed(tp, state, info);
Benny Prijono21407322010-03-10 13:33:25 +00001590}
1591
Benny Prijono23674a32007-12-01 08:59:25 +00001592/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001593 * Create and initialize SIP socket (and possibly resolve public
1594 * address via STUN, depending on config).
1595 */
Benny Prijono23674a32007-12-01 08:59:25 +00001596static pj_status_t create_sip_udp_sock(int af,
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001597 const pjsua_transport_config *cfg,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001598 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001599 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001600{
Benny Prijono23674a32007-12-01 08:59:25 +00001601 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001602 unsigned port = cfg->port;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001603 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001604 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001605 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001606 pj_status_t status;
1607
Benny Prijonoc97608e2007-03-23 16:34:20 +00001608 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001609 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001610 if (status != PJ_SUCCESS) {
1611 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1612 return status;
1613 }
1614
Benny Prijono23674a32007-12-01 08:59:25 +00001615 /* Initialize bound address */
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001616 if (cfg->bound_addr.slen) {
1617 status = pj_sockaddr_init(af, &bind_addr, &cfg->bound_addr,
Benny Prijono23674a32007-12-01 08:59:25 +00001618 (pj_uint16_t)port);
1619 if (status != PJ_SUCCESS) {
1620 pjsua_perror(THIS_FILE,
1621 "Unable to resolve transport bound address",
1622 status);
1623 return status;
1624 }
1625 } else {
1626 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1627 }
1628
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001629 /* Create socket */
Benny Prijono23674a32007-12-01 08:59:25 +00001630 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001631 if (status != PJ_SUCCESS) {
1632 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001633 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001634 }
1635
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001636 /* Apply QoS, if specified */
1637 status = pj_sock_apply_qos2(sock, cfg->qos_type,
1638 &cfg->qos_params,
1639 2, THIS_FILE, "SIP UDP socket");
1640
1641 /* Bind socket */
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001642 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001643 if (status != PJ_SUCCESS) {
1644 pjsua_perror(THIS_FILE, "bind() error", status);
1645 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001646 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001647 }
1648
Benny Prijonoe347cb02007-02-14 14:36:13 +00001649 /* If port is zero, get the bound port */
1650 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001651 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001652 int namelen = sizeof(bound_addr);
1653 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1654 if (status != PJ_SUCCESS) {
1655 pjsua_perror(THIS_FILE, "getsockname() error", status);
1656 pj_sock_close(sock);
1657 return status;
1658 }
1659
Benny Prijono23674a32007-12-01 08:59:25 +00001660 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001661 }
1662
Benny Prijonoc97608e2007-03-23 16:34:20 +00001663 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001664 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1665 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001666 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001667 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001668 }
1669
1670 /* Get the published address, either by STUN or by resolving
1671 * the name of local host.
1672 */
Benny Prijono23674a32007-12-01 08:59:25 +00001673 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001674 /*
1675 * Public address is already specified, no need to resolve the
1676 * address, only set the port.
1677 */
Benny Prijono23674a32007-12-01 08:59:25 +00001678 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1679 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001680
1681 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001682 /*
1683 * STUN is specified, resolve the address with STUN.
1684 */
Benny Prijono23674a32007-12-01 08:59:25 +00001685 if (af != pj_AF_INET()) {
1686 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1687 pj_sock_close(sock);
1688 return PJ_EAFNOTSUP;
1689 }
1690
Benny Prijono14c2b862007-02-21 00:40:05 +00001691 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001692 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1693 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001694 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001695 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001696 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001697 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001698 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001699 }
1700
1701 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001702 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001703
Benny Prijono42d08d22007-12-20 11:23:07 +00001704 if (pj_sockaddr_has_addr(&bind_addr)) {
1705 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1706 } else {
1707 status = pj_gethostip(af, p_pub_addr);
1708 if (status != PJ_SUCCESS) {
1709 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1710 pj_sock_close(sock);
1711 return status;
1712 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001713 }
1714
Benny Prijono23674a32007-12-01 08:59:25 +00001715 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1716 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001717 }
1718
1719 *p_sock = sock;
1720
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001721 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001722 addr_string(p_pub_addr),
1723 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001724
Benny Prijonod8410532006-06-15 11:04:33 +00001725 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001726}
1727
1728
1729/*
1730 * Create SIP transport.
1731 */
1732PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1733 const pjsua_transport_config *cfg,
1734 pjsua_transport_id *p_id)
1735{
1736 pjsip_transport *tp;
1737 unsigned id;
1738 pj_status_t status;
1739
1740 PJSUA_LOCK();
1741
1742 /* Find empty transport slot */
1743 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001744 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001745 break;
1746 }
1747
1748 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1749 status = PJ_ETOOMANY;
1750 pjsua_perror(THIS_FILE, "Error creating transport", status);
1751 goto on_return;
1752 }
1753
1754 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001755 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001756 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001757 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001758 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001759 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001760 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001761 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001762 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001763 pjsip_host_port addr_name;
1764
1765 /* Supply default config if it's not specified */
1766 if (cfg == NULL) {
1767 pjsua_transport_config_default(&config);
1768 cfg = &config;
1769 }
1770
Benny Prijono0a5cad82006-09-26 13:21:02 +00001771 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001772 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1773 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001774 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001775 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1776 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001777 if (status != PJ_SUCCESS) {
1778 pjsua_perror(THIS_FILE,
1779 "Unable to resolve transport public address",
1780 status);
1781 goto on_return;
1782 }
1783 }
1784
1785 /* Create the socket and possibly resolve the address with STUN
1786 * (only when public address is not specified).
1787 */
Benny Prijono23674a32007-12-01 08:59:25 +00001788 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001789 cfg, &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001790 if (status != PJ_SUCCESS)
1791 goto on_return;
1792
Benny Prijono23674a32007-12-01 08:59:25 +00001793 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1794 addr_name.host = pj_str(hostbuf);
1795 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001796
1797 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001798 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1799 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001800 if (status != PJ_SUCCESS) {
1801 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1802 status);
1803 pj_sock_close(sock);
1804 goto on_return;
1805 }
1806
Benny Prijonoe93e2872006-06-28 16:46:49 +00001807
1808 /* Save the transport */
1809 pjsua_var.tpdata[id].type = type;
1810 pjsua_var.tpdata[id].local_name = tp->local_name;
1811 pjsua_var.tpdata[id].data.tp = tp;
1812
Benny Prijono3569c0d2007-04-06 10:29:20 +00001813#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1814
Benny Prijonob2477142007-12-05 04:09:59 +00001815 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001816 /*
1817 * Create TCP transport.
1818 */
1819 pjsua_transport_config config;
1820 pjsip_tpfactory *tcp;
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001821 pjsip_tcp_transport_cfg tcp_cfg;
1822
1823 pjsip_tcp_transport_cfg_default(&tcp_cfg, pj_AF_INET());
Benny Prijonoe93e2872006-06-28 16:46:49 +00001824
1825 /* Supply default config if it's not specified */
1826 if (cfg == NULL) {
1827 pjsua_transport_config_default(&config);
1828 cfg = &config;
1829 }
1830
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001831 /* Configure bind address */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001832 if (cfg->port)
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001833 pj_sockaddr_set_port(&tcp_cfg.bind_addr, (pj_uint16_t)cfg->port);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001834
Benny Prijono0a5cad82006-09-26 13:21:02 +00001835 if (cfg->bound_addr.slen) {
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001836 status = pj_sockaddr_set_str_addr(tcp_cfg.af,
1837 &tcp_cfg.bind_addr,
1838 &cfg->bound_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001839 if (status != PJ_SUCCESS) {
1840 pjsua_perror(THIS_FILE,
1841 "Unable to resolve transport bound address",
1842 status);
1843 goto on_return;
1844 }
1845 }
1846
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001847 /* Set published name */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001848 if (cfg->public_addr.slen)
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001849 tcp_cfg.addr_name.host = cfg->public_addr;
1850
1851 /* Copy the QoS settings */
1852 tcp_cfg.qos_type = cfg->qos_type;
1853 pj_memcpy(&tcp_cfg.qos_params, &cfg->qos_params,
1854 sizeof(cfg->qos_params));
Benny Prijonoe93e2872006-06-28 16:46:49 +00001855
1856 /* Create the TCP transport */
Benny Prijono4d79b0f2009-10-25 09:02:07 +00001857 status = pjsip_tcp_transport_start3(pjsua_var.endpt, &tcp_cfg, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001858
1859 if (status != PJ_SUCCESS) {
1860 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1861 status);
1862 goto on_return;
1863 }
1864
1865 /* Save the transport */
1866 pjsua_var.tpdata[id].type = type;
1867 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1868 pjsua_var.tpdata[id].data.factory = tcp;
1869
Benny Prijono3569c0d2007-04-06 10:29:20 +00001870#endif /* PJ_HAS_TCP */
1871
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001872#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1873 } else if (type == PJSIP_TRANSPORT_TLS) {
1874 /*
1875 * Create TLS transport.
1876 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001877 pjsua_transport_config config;
1878 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001879 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001880 pj_sockaddr_in local_addr;
1881
1882 /* Supply default config if it's not specified */
1883 if (cfg == NULL) {
1884 pjsua_transport_config_default(&config);
1885 config.port = 5061;
1886 cfg = &config;
1887 }
1888
1889 /* Init local address */
1890 pj_sockaddr_in_init(&local_addr, 0, 0);
1891
1892 if (cfg->port)
1893 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1894
1895 if (cfg->bound_addr.slen) {
1896 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1897 if (status != PJ_SUCCESS) {
1898 pjsua_perror(THIS_FILE,
1899 "Unable to resolve transport bound address",
1900 status);
1901 goto on_return;
1902 }
1903 }
1904
1905 /* Init published name */
1906 pj_bzero(&a_name, sizeof(pjsip_host_port));
1907 if (cfg->public_addr.slen)
1908 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001909
1910 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001911 &cfg->tls_setting,
1912 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001913 if (status != PJ_SUCCESS) {
1914 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1915 status);
1916 goto on_return;
1917 }
1918
1919 /* Save the transport */
1920 pjsua_var.tpdata[id].type = type;
1921 pjsua_var.tpdata[id].local_name = tls->addr_name;
1922 pjsua_var.tpdata[id].data.factory = tls;
1923#endif
1924
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001925 } else {
1926 status = PJSIP_EUNSUPTRANSPORT;
1927 pjsua_perror(THIS_FILE, "Error creating transport", status);
1928 goto on_return;
1929 }
1930
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +00001931 /* Set transport state callback */
1932 if (pjsua_var.ua_cfg.cb.on_transport_state) {
Benny Prijono21407322010-03-10 13:33:25 +00001933 pjsip_tp_state_callback tpcb;
1934 pjsip_tpmgr *tpmgr;
1935
1936 tpmgr = pjsip_endpt_get_tpmgr(pjsua_var.endpt);
Nanang Izzuddin91cfa962010-04-14 06:57:35 +00001937 tpcb = pjsip_tpmgr_get_state_cb(tpmgr);
Benny Prijono21407322010-03-10 13:33:25 +00001938
1939 if (tpcb != &on_tp_state_callback) {
1940 pjsua_var.old_tp_cb = tpcb;
Nanang Izzuddin91cfa962010-04-14 06:57:35 +00001941 pjsip_tpmgr_set_state_cb(tpmgr, &on_tp_state_callback);
Benny Prijono21407322010-03-10 13:33:25 +00001942 }
Nanang Izzuddin2fb937e2010-02-24 05:43:34 +00001943 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001944
1945 /* Return the ID */
1946 if (p_id) *p_id = id;
1947
1948 status = PJ_SUCCESS;
1949
1950on_return:
1951
1952 PJSUA_UNLOCK();
1953
Benny Prijonod8410532006-06-15 11:04:33 +00001954 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001955}
1956
1957
1958/*
1959 * Register transport that has been created by application.
1960 */
1961PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1962 pjsua_transport_id *p_id)
1963{
1964 unsigned id;
1965
1966 PJSUA_LOCK();
1967
1968 /* Find empty transport slot */
1969 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001970 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001971 break;
1972 }
1973
1974 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1975 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1976 PJSUA_UNLOCK();
1977 return PJ_ETOOMANY;
1978 }
1979
1980 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001981 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001982 pjsua_var.tpdata[id].local_name = tp->local_name;
1983 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001984
1985 /* Return the ID */
1986 if (p_id) *p_id = id;
1987
1988 PJSUA_UNLOCK();
1989
1990 return PJ_SUCCESS;
1991}
1992
1993
1994/*
1995 * Enumerate all transports currently created in the system.
1996 */
1997PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1998 unsigned *p_count )
1999{
2000 unsigned i, count;
2001
2002 PJSUA_LOCK();
2003
2004 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
2005 ++i)
2006 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00002007 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002008 continue;
2009
2010 id[count++] = i;
2011 }
2012
2013 *p_count = count;
2014
2015 PJSUA_UNLOCK();
2016
2017 return PJ_SUCCESS;
2018}
2019
2020
2021/*
2022 * Get information about transports.
2023 */
2024PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
2025 pjsua_transport_info *info)
2026{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002027 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00002028 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002029
Benny Prijonoac623b32006-07-03 15:19:31 +00002030 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002031
2032 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002033 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
2034 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002035
2036 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00002037 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002038
2039 PJSUA_LOCK();
2040
Benny Prijonoe93e2872006-06-28 16:46:49 +00002041 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
2042
2043 pjsip_transport *tp = t->data.tp;
2044
2045 if (tp == NULL) {
2046 PJSUA_UNLOCK();
2047 return PJ_EINVALIDOP;
2048 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002049
Benny Prijonoe93e2872006-06-28 16:46:49 +00002050 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00002051 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00002052 info->type_name = pj_str(tp->type_name);
2053 info->info = pj_str(tp->info);
2054 info->flag = tp->flag;
2055 info->addr_len = tp->addr_len;
2056 info->local_addr = tp->local_addr;
2057 info->local_name = tp->local_name;
2058 info->usage_count = pj_atomic_get(tp->ref_cnt);
2059
Benny Prijono0a5cad82006-09-26 13:21:02 +00002060 status = PJ_SUCCESS;
2061
Benny Prijonoe93e2872006-06-28 16:46:49 +00002062 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
2063
2064 pjsip_tpfactory *factory = t->data.factory;
2065
2066 if (factory == NULL) {
2067 PJSUA_UNLOCK();
2068 return PJ_EINVALIDOP;
2069 }
2070
2071 info->id = id;
2072 info->type = t->type;
2073 info->type_name = pj_str("TCP");
2074 info->info = pj_str("TCP transport");
2075 info->flag = factory->flag;
2076 info->addr_len = sizeof(factory->local_addr);
2077 info->local_addr = factory->local_addr;
2078 info->local_name = factory->addr_name;
2079 info->usage_count = 0;
2080
Benny Prijono0a5cad82006-09-26 13:21:02 +00002081 status = PJ_SUCCESS;
2082
2083 } else {
2084 pj_assert(!"Unsupported transport");
2085 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00002086 }
2087
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002088
2089 PJSUA_UNLOCK();
2090
Benny Prijono0a5cad82006-09-26 13:21:02 +00002091 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002092}
2093
2094
2095/*
2096 * Disable a transport or re-enable it.
2097 */
2098PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
2099 pj_bool_t enabled)
2100{
2101 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002102 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
2103 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002104
2105 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00002106 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002107
2108
2109 /* To be done!! */
2110 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00002111 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002112
2113 return PJ_EINVALIDOP;
2114}
2115
2116
2117/*
2118 * Close the transport.
2119 */
2120PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
2121 pj_bool_t force )
2122{
Benny Prijono5ff61872007-02-01 03:37:11 +00002123 pj_status_t status;
2124
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002125 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00002126 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
2127 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002128
2129 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00002130 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002131
Benny Prijono0a5cad82006-09-26 13:21:02 +00002132 /* Note: destroy() may not work if there are objects still referencing
2133 * the transport.
2134 */
2135 if (force) {
2136 switch (pjsua_var.tpdata[id].type) {
2137 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00002138 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
2139 if (status != PJ_SUCCESS)
2140 return status;
2141 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
2142 if (status != PJ_SUCCESS)
2143 return status;
2144 break;
2145
2146 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00002147 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00002148 /* This will close the TCP listener, but existing TCP/TLS
2149 * connections (if any) will still linger
2150 */
2151 status = (*pjsua_var.tpdata[id].data.factory->destroy)
2152 (pjsua_var.tpdata[id].data.factory);
2153 if (status != PJ_SUCCESS)
2154 return status;
2155
Benny Prijono0a5cad82006-09-26 13:21:02 +00002156 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00002157
Benny Prijono1ebd6142006-10-19 15:48:02 +00002158 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00002159 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00002160 }
2161
2162 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00002163 /* If force is not specified, transports will be closed at their
2164 * convenient time. However this will leak PJSUA-API transport
2165 * descriptors as PJSUA-API wouldn't know when exactly the
2166 * transport is closed thus it can't cleanup PJSUA transport
2167 * descriptor.
2168 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00002169 switch (pjsua_var.tpdata[id].type) {
2170 case PJSIP_TRANSPORT_UDP:
2171 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00002172 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00002173 case PJSIP_TRANSPORT_TCP:
2174 return (*pjsua_var.tpdata[id].data.factory->destroy)
2175 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002176 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00002177 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00002178 }
2179 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002180
Benny Prijono5ff61872007-02-01 03:37:11 +00002181 /* Cleanup pjsua data when force is applied */
2182 if (force) {
2183 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
2184 pjsua_var.tpdata[id].data.ptr = NULL;
2185 }
2186
2187 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002188}
2189
2190
2191/*
2192 * Add additional headers etc in msg_data specified by application
2193 * when sending requests.
2194 */
2195void pjsua_process_msg_data(pjsip_tx_data *tdata,
2196 const pjsua_msg_data *msg_data)
2197{
2198 pj_bool_t allow_body;
2199 const pjsip_hdr *hdr;
2200
Benny Prijono56315612006-07-18 14:39:40 +00002201 /* Always add User-Agent */
2202 if (pjsua_var.ua_cfg.user_agent.slen &&
2203 tdata->msg->type == PJSIP_REQUEST_MSG)
2204 {
2205 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
2206 pjsip_hdr *h;
2207 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
2208 &STR_USER_AGENT,
2209 &pjsua_var.ua_cfg.user_agent);
2210 pjsip_msg_add_hdr(tdata->msg, h);
2211 }
2212
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002213 if (!msg_data)
2214 return;
2215
2216 hdr = msg_data->hdr_list.next;
2217 while (hdr && hdr != &msg_data->hdr_list) {
2218 pjsip_hdr *new_hdr;
2219
Benny Prijonoa1e69682007-05-11 15:14:34 +00002220 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002221 pjsip_msg_add_hdr(tdata->msg, new_hdr);
2222
2223 hdr = hdr->next;
2224 }
2225
2226 allow_body = (tdata->msg->body == NULL);
2227
2228 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
2229 pjsip_media_type ctype;
2230 pjsip_msg_body *body;
2231
2232 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
2233 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
2234 &msg_data->msg_body);
2235 tdata->msg->body = body;
2236 }
Benny Prijono1c1d7342010-08-01 09:48:51 +00002237
2238 /* Multipart */
2239 if (!pj_list_empty(&msg_data->multipart_parts) &&
2240 msg_data->multipart_ctype.type.slen)
2241 {
2242 pjsip_msg_body *bodies;
2243 pjsip_multipart_part *part;
2244 pj_str_t *boundary = NULL;
2245
2246 bodies = pjsip_multipart_create(tdata->pool,
2247 &msg_data->multipart_ctype,
2248 boundary);
2249 part = msg_data->multipart_parts.next;
2250 while (part != &msg_data->multipart_parts) {
2251 pjsip_multipart_part *part_copy;
2252
2253 part_copy = pjsip_multipart_clone_part(tdata->pool, part);
2254 pjsip_multipart_add_part(tdata->pool, bodies, part_copy);
2255 part = part->next;
2256 }
2257
2258 if (tdata->msg->body) {
2259 part = pjsip_multipart_create_part(tdata->pool);
2260 part->body = tdata->msg->body;
2261 pjsip_multipart_add_part(tdata->pool, bodies, part);
2262
2263 tdata->msg->body = NULL;
2264 }
2265
2266 tdata->msg->body = bodies;
2267 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002268}
2269
2270
2271/*
2272 * Add route_set to outgoing requests
2273 */
2274void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
2275 const pjsip_route_hdr *route_set )
2276{
2277 const pjsip_route_hdr *r;
2278
2279 r = route_set->next;
2280 while (r != route_set) {
2281 pjsip_route_hdr *new_r;
2282
Benny Prijonoa1e69682007-05-11 15:14:34 +00002283 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002284 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
2285
2286 r = r->next;
2287 }
2288}
2289
2290
2291/*
2292 * Simple version of MIME type parsing (it doesn't support parameters)
2293 */
2294void pjsua_parse_media_type( pj_pool_t *pool,
2295 const pj_str_t *mime,
2296 pjsip_media_type *media_type)
2297{
2298 pj_str_t tmp;
2299 char *pos;
2300
Benny Prijonoac623b32006-07-03 15:19:31 +00002301 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002302
2303 pj_strdup_with_null(pool, &tmp, mime);
2304
2305 pos = pj_strchr(&tmp, '/');
2306 if (pos) {
2307 media_type->type.ptr = tmp.ptr;
2308 media_type->type.slen = (pos-tmp.ptr);
2309 media_type->subtype.ptr = pos+1;
2310 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
2311 } else {
2312 media_type->type = tmp;
2313 }
2314}
2315
2316
2317/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002318 * Internal function to init transport selector from transport id.
2319 */
2320void pjsua_init_tpselector(pjsua_transport_id tp_id,
2321 pjsip_tpselector *sel)
2322{
2323 pjsua_transport_data *tpdata;
2324 unsigned flag;
2325
2326 pj_bzero(sel, sizeof(*sel));
2327 if (tp_id == PJSUA_INVALID_ID)
2328 return;
2329
Benny Prijonoa1e69682007-05-11 15:14:34 +00002330 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002331 tpdata = &pjsua_var.tpdata[tp_id];
2332
2333 flag = pjsip_transport_get_flag_from_type(tpdata->type);
2334
2335 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
2336 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
2337 sel->u.transport = tpdata->data.tp;
2338 } else {
2339 sel->type = PJSIP_TPSELECTOR_LISTENER;
2340 sel->u.listener = tpdata->data.factory;
2341 }
2342}
2343
2344
Benny Prijono6ba8c542007-10-16 01:34:14 +00002345/* Callback upon NAT detection completion */
2346static void nat_detect_cb(void *user_data,
2347 const pj_stun_nat_detect_result *res)
2348{
2349 PJ_UNUSED_ARG(user_data);
2350
2351 pjsua_var.nat_in_progress = PJ_FALSE;
2352 pjsua_var.nat_status = res->status;
2353 pjsua_var.nat_type = res->nat_type;
2354
2355 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
2356 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
2357 }
2358}
2359
2360
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002361/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002362 * Detect NAT type.
2363 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00002364PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002365{
2366 pj_status_t status;
2367
Benny Prijono6ba8c542007-10-16 01:34:14 +00002368 if (pjsua_var.nat_in_progress)
2369 return PJ_SUCCESS;
2370
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002371 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +00002372 status = resolve_stun_server(PJ_TRUE);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002373 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002374 pjsua_var.nat_status = status;
2375 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002376 return status;
2377 }
2378
2379 /* Make sure we have STUN */
2380 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002381 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
2382 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002383 }
2384
Benny Prijono6ba8c542007-10-16 01:34:14 +00002385 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
2386 &pjsua_var.stun_cfg,
2387 NULL, &nat_detect_cb);
2388
2389 if (status != PJ_SUCCESS) {
2390 pjsua_var.nat_status = status;
2391 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
2392 return status;
2393 }
2394
2395 pjsua_var.nat_in_progress = PJ_TRUE;
2396
2397 return PJ_SUCCESS;
2398}
2399
2400
2401/*
2402 * Get NAT type.
2403 */
2404PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
2405{
2406 *type = pjsua_var.nat_type;
2407 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002408}
2409
Benny Prijonoc7545782010-09-28 07:43:18 +00002410/*
2411 * Verify that valid url is given.
2412 */
2413PJ_DEF(pj_status_t) pjsua_verify_url(const char *c_url)
2414{
2415 pjsip_uri *p;
2416 pj_pool_t *pool;
2417 char *url;
2418 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2419
2420 if (!len) return PJSIP_EINVALIDURI;
2421
2422 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
2423 if (!pool) return PJ_ENOMEM;
2424
2425 url = (char*) pj_pool_alloc(pool, len+1);
2426 pj_ansi_strcpy(url, c_url);
2427
2428 p = pjsip_parse_uri(pool, url, len, 0);
2429
2430 pj_pool_release(pool);
2431 return p ? 0 : PJSIP_EINVALIDURI;
2432}
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002433
2434/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002435 * Verify that valid SIP url is given.
2436 */
2437PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
2438{
2439 pjsip_uri *p;
2440 pj_pool_t *pool;
2441 char *url;
2442 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2443
Benny Prijonoc7545782010-09-28 07:43:18 +00002444 if (!len) return PJSIP_EINVALIDURI;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002445
2446 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
Benny Prijonoc7545782010-09-28 07:43:18 +00002447 if (!pool) return PJ_ENOMEM;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002448
Benny Prijonoa1e69682007-05-11 15:14:34 +00002449 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002450 pj_ansi_strcpy(url, c_url);
2451
2452 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002453 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
2454 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
2455 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002456 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002457 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002458
2459 pj_pool_release(pool);
Benny Prijonoc7545782010-09-28 07:43:18 +00002460 return p ? 0 : PJSIP_EINVALIDURI;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002461}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002462
Benny Prijono73bb7232009-10-20 13:56:26 +00002463/*
2464 * Schedule a timer entry.
2465 */
2466PJ_DEF(pj_status_t) pjsua_schedule_timer( pj_timer_entry *entry,
2467 const pj_time_val *delay)
2468{
2469 return pjsip_endpt_schedule_timer(pjsua_var.endpt, entry, delay);
2470}
2471
2472/*
2473 * Cancel the previously scheduled timer.
2474 *
2475 */
2476PJ_DEF(void) pjsua_cancel_timer(pj_timer_entry *entry)
2477{
2478 pjsip_endpt_cancel_timer(pjsua_var.endpt, entry);
2479}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002480
Benny Prijono91d06b62008-09-20 12:16:56 +00002481/**
2482 * Normalize route URI (check for ";lr" and append one if it doesn't
2483 * exist and pjsua_config.force_lr is set.
2484 */
2485pj_status_t normalize_route_uri(pj_pool_t *pool, pj_str_t *uri)
2486{
2487 pj_str_t tmp_uri;
2488 pj_pool_t *tmp_pool;
2489 pjsip_uri *uri_obj;
2490 pjsip_sip_uri *sip_uri;
2491
2492 tmp_pool = pjsua_pool_create("tmplr%p", 512, 512);
2493 if (!tmp_pool)
2494 return PJ_ENOMEM;
2495
2496 pj_strdup_with_null(tmp_pool, &tmp_uri, uri);
2497
2498 uri_obj = pjsip_parse_uri(tmp_pool, tmp_uri.ptr, tmp_uri.slen, 0);
2499 if (!uri_obj) {
2500 PJ_LOG(1,(THIS_FILE, "Invalid route URI: %.*s",
2501 (int)uri->slen, uri->ptr));
2502 pj_pool_release(tmp_pool);
2503 return PJSIP_EINVALIDURI;
2504 }
2505
2506 if (!PJSIP_URI_SCHEME_IS_SIP(uri_obj) &&
2507 !PJSIP_URI_SCHEME_IS_SIP(uri_obj))
2508 {
2509 PJ_LOG(1,(THIS_FILE, "Route URI must be SIP URI: %.*s",
2510 (int)uri->slen, uri->ptr));
2511 pj_pool_release(tmp_pool);
2512 return PJSIP_EINVALIDSCHEME;
2513 }
2514
2515 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri_obj);
2516
2517 /* Done if force_lr is disabled or if lr parameter is present */
2518 if (!pjsua_var.ua_cfg.force_lr || sip_uri->lr_param) {
2519 pj_pool_release(tmp_pool);
2520 return PJ_SUCCESS;
2521 }
2522
2523 /* Set lr param */
2524 sip_uri->lr_param = 1;
2525
2526 /* Print the URI */
2527 tmp_uri.ptr = (char*) pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE);
2528 tmp_uri.slen = pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, uri_obj,
2529 tmp_uri.ptr, PJSIP_MAX_URL_SIZE);
2530 if (tmp_uri.slen < 1) {
2531 PJ_LOG(1,(THIS_FILE, "Route URI is too long: %.*s",
2532 (int)uri->slen, uri->ptr));
2533 pj_pool_release(tmp_pool);
2534 return PJSIP_EURITOOLONG;
2535 }
2536
2537 /* Clone the URI */
2538 pj_strdup_with_null(pool, uri, &tmp_uri);
2539
Benny Prijonodc20c592009-10-15 06:55:30 +00002540 pj_pool_release(tmp_pool);
Benny Prijono91d06b62008-09-20 12:16:56 +00002541 return PJ_SUCCESS;
2542}
2543
Benny Prijonoda9785b2007-04-02 20:43:06 +00002544/*
2545 * This is a utility function to dump the stack states to log, using
2546 * verbosity level 3.
2547 */
2548PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2549{
2550 unsigned old_decor;
2551 unsigned i;
Benny Prijonoda9785b2007-04-02 20:43:06 +00002552
2553 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2554
2555 old_decor = pj_log_get_decor();
2556 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2557
2558 if (detail)
2559 pj_dump_config();
2560
2561 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2562
2563 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2564
2565 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2566 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2567 pjsua_call *call = &pjsua_var.calls[i];
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002568 pjmedia_transport_info tpinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002569 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002570
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002571 /* MSVC complains about tpinfo not being initialized */
Benny Prijono734fc2d2008-03-17 16:05:35 +00002572 //pj_bzero(&tpinfo, sizeof(tpinfo));
Benny Prijono4f093d22007-04-09 08:54:32 +00002573
Benny Prijono734fc2d2008-03-17 16:05:35 +00002574 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002575 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002576
Benny Prijono5186eae2007-12-03 14:38:25 +00002577 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002578 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002579 pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, addr_buf,
Benny Prijono5186eae2007-12-03 14:38:25 +00002580 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002581 }
2582
2583 pjsip_tsx_layer_dump(detail);
2584 pjsip_ua_dump(detail);
2585
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002586// Dumping complete call states may require a 'large' buffer
2587// (about 3KB per call session, including RTCP XR).
2588#if 0
Benny Prijonoda9785b2007-04-02 20:43:06 +00002589 /* Dump all invite sessions: */
2590 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2591
2592 if (pjsua_call_get_count() == 0) {
2593
2594 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2595
2596 } else {
2597 unsigned i;
2598
2599 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2600 if (pjsua_call_is_active(i)) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002601 /* Tricky logging, since call states log string tends to be
2602 * longer than PJ_LOG_MAX_SIZE.
2603 */
2604 char buf[1024 * 3];
2605 unsigned call_dump_len;
2606 unsigned part_len;
2607 unsigned part_idx;
2608 unsigned log_decor;
2609
Benny Prijonoda9785b2007-04-02 20:43:06 +00002610 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002611 call_dump_len = strlen(buf);
2612
2613 log_decor = pj_log_get_decor();
2614 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE |
2615 PJ_LOG_HAS_CR));
2616 PJ_LOG(3,(THIS_FILE, "\n"));
2617 pj_log_set_decor(0);
2618
2619 part_idx = 0;
2620 part_len = PJ_LOG_MAX_SIZE-80;
2621 while (part_idx < call_dump_len) {
2622 char p_orig, *p;
2623
2624 p = &buf[part_idx];
2625 if (part_idx + part_len > call_dump_len)
2626 part_len = call_dump_len - part_idx;
2627 p_orig = p[part_len];
2628 p[part_len] = '\0';
2629 PJ_LOG(3,(THIS_FILE, "%s", p));
2630 p[part_len] = p_orig;
2631 part_idx += part_len;
2632 }
2633 pj_log_set_decor(log_decor);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002634 }
2635 }
2636 }
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002637#endif
Benny Prijonoda9785b2007-04-02 20:43:06 +00002638
2639 /* Dump presence status */
2640 pjsua_pres_dump(detail);
2641
2642 pj_log_set_decor(old_decor);
2643 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2644}
2645