blob: 0b24cb5c5598a87c0a034ae8a98d511cf2c24114 [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 Prijonod64c1502009-08-17 10:42:55 +000067
68 pjsua_config_default(&pjsua_var.ua_cfg);
Benny Prijonoeebe9af2006-06-13 22:57:13 +000069}
Benny Prijonodc39fe82006-05-26 12:17:46 +000070
71
Benny Prijono1f61a8f2007-08-16 10:11:44 +000072PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
73{
74 pj_bzero(cfg, sizeof(*cfg));
75
76 cfg->msg_logging = PJ_TRUE;
77 cfg->level = 5;
78 cfg->console_level = 4;
79 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
Benny Prijonod6e362a2008-07-19 17:53:47 +000080 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE |
81 PJ_LOG_HAS_SPACE;
82#if defined(PJ_WIN32) && PJ_WIN32 != 0
83 cfg->decor |= PJ_LOG_HAS_COLOR;
84#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +000085}
86
87PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
88 pjsua_logging_config *dst,
89 const pjsua_logging_config *src)
90{
91 pj_memcpy(dst, src, sizeof(*src));
92 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
93}
94
95PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
96{
97 pj_bzero(cfg, sizeof(*cfg));
98
Benny Prijono6627f3e2009-02-10 11:08:54 +000099 cfg->max_calls = ((PJSUA_MAX_CALLS) < 4) ? (PJSUA_MAX_CALLS) : 4;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000100 cfg->thread_cnt = 1;
Benny Prijono91a6a172007-10-31 08:59:29 +0000101 cfg->nat_type_in_sdp = 1;
Benny Prijonobb995fd2009-08-12 11:03:23 +0000102 cfg->stun_ignore_failure = PJ_TRUE;
Benny Prijono91d06b62008-09-20 12:16:56 +0000103 cfg->force_lr = PJ_TRUE;
Benny Prijonod8179652008-01-23 20:39:07 +0000104#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
105 cfg->use_srtp = PJSUA_DEFAULT_USE_SRTP;
106 cfg->srtp_secure_signaling = PJSUA_DEFAULT_SRTP_SECURE_SIGNALING;
107#endif
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000108 cfg->hangup_forked_call = PJ_TRUE;
Nanang Izzuddin59dffb12009-08-11 12:42:38 +0000109
Nanang Izzuddin65add622009-08-11 16:26:20 +0000110 pjsip_timer_setting_default(&cfg->timer_setting);
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000111}
112
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000113PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
114 pjsua_config *dst,
115 const pjsua_config *src)
116{
117 unsigned i;
118
119 pj_memcpy(dst, src, sizeof(*src));
120
121 for (i=0; i<src->outbound_proxy_cnt; ++i) {
122 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
123 &src->outbound_proxy[i]);
124 }
125
126 for (i=0; i<src->cred_count; ++i) {
127 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
128 }
129
130 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
131 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
132 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
Benny Prijonobb995fd2009-08-12 11:03:23 +0000133
134 for (i=0; i<src->stun_srv_cnt; ++i) {
135 pj_strdup_with_null(pool, &dst->stun_srv[i], &src->stun_srv[i]);
136 }
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000137}
138
139PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
140{
141 pj_bzero(msg_data, sizeof(*msg_data));
142 pj_list_init(&msg_data->hdr_list);
143}
144
145PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
146{
147 pj_bzero(cfg, sizeof(*cfg));
148 pjsip_tls_setting_default(&cfg->tls_setting);
149}
150
151PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
152 pjsua_transport_config *dst,
153 const pjsua_transport_config *src)
154{
155 PJ_UNUSED_ARG(pool);
156 pj_memcpy(dst, src, sizeof(*src));
157}
158
159PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
160{
161 pj_bzero(cfg, sizeof(*cfg));
162
163 cfg->reg_timeout = PJSUA_REG_INTERVAL;
164 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000165 cfg->allow_contact_rewrite = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000166 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Nanang Izzuddin59dffb12009-08-11 12:42:38 +0000167 cfg->require_timer = pjsua_var.ua_cfg.require_timer;
Nanang Izzuddin65add622009-08-11 16:26:20 +0000168 cfg->timer_setting = pjsua_var.ua_cfg.timer_setting;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000169 cfg->ka_interval = 15;
170 cfg->ka_data = pj_str("\r\n");
Benny Prijonod8179652008-01-23 20:39:07 +0000171#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
172 cfg->use_srtp = pjsua_var.ua_cfg.use_srtp;
173 cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling;
174#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000175}
176
177PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
178{
179 pj_bzero(cfg, sizeof(*cfg));
180}
181
182PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
183{
184 pj_bzero(cfg, sizeof(*cfg));
185
186 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono50f19b32008-03-11 13:15:43 +0000187 cfg->snd_clock_rate = 0;
Benny Prijono7d60d052008-03-29 12:24:20 +0000188 cfg->channel_count = 1;
Benny Prijono37c710b2008-01-10 12:09:26 +0000189 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
190 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000191 cfg->has_ioqueue = PJ_TRUE;
192 cfg->thread_cnt = 1;
193 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
194 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
195 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
Benny Prijono10454dc2009-02-21 14:21:59 +0000196 cfg->snd_rec_latency = PJMEDIA_SND_DEFAULT_REC_LATENCY;
197 cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000198 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
Benny Prijonof798e502009-03-09 13:08:16 +0000199 cfg->snd_auto_close_time = 1;
Benny Prijonof76e1392008-06-06 14:51:48 +0000200
Benny Prijono329d6382009-05-29 13:04:03 +0000201 cfg->ice_max_host_cands = -1;
202 pj_ice_sess_options_default(&cfg->ice_opt);
203
Benny Prijonof76e1392008-06-06 14:51:48 +0000204 cfg->turn_conn_type = PJ_TURN_TP_UDP;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000205}
206
Benny Prijonodc39fe82006-05-26 12:17:46 +0000207
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000208/*****************************************************************************
209 * This is a very simple PJSIP module, whose sole purpose is to display
210 * incoming and outgoing messages to log. This module will have priority
211 * higher than transport layer, which means:
212 *
213 * - incoming messages will come to this module first before reaching
214 * transaction layer.
215 *
216 * - outgoing messages will come to this module last, after the message
217 * has been 'printed' to contiguous buffer by transport layer and
218 * appropriate transport instance has been decided for this message.
219 *
220 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000221
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000222/* Notification on incoming messages */
223static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
224{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000225 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
226 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000227 "--end msg--",
228 rdata->msg_info.len,
229 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000230 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000231 rdata->pkt_info.src_name,
232 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000233 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000234 rdata->msg_info.msg_buf));
235
236 /* Always return false, otherwise messages will not get processed! */
237 return PJ_FALSE;
238}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000239
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240/* Notification on outgoing messages */
241static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
242{
243
244 /* Important note:
245 * tp_info field is only valid after outgoing messages has passed
246 * transport layer. So don't try to access tp_info when the module
247 * has lower priority than transport layer.
248 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000249
Benny Prijonob4a17c92006-07-10 14:40:21 +0000250 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
251 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000252 "--end msg--",
253 (tdata->buf.cur - tdata->buf.start),
254 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000255 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000256 tdata->tp_info.dst_name,
257 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000258 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000259 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000260
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000261 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000262 return PJ_SUCCESS;
263}
264
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000265/* The module instance. */
266static pjsip_module pjsua_msg_logger =
267{
268 NULL, NULL, /* prev, next. */
269 { "mod-pjsua-log", 13 }, /* Name. */
270 -1, /* Id */
271 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
272 NULL, /* load() */
273 NULL, /* start() */
274 NULL, /* stop() */
275 NULL, /* unload() */
276 &logging_on_rx_msg, /* on_rx_request() */
277 &logging_on_rx_msg, /* on_rx_response() */
278 &logging_on_tx_msg, /* on_tx_request. */
279 &logging_on_tx_msg, /* on_tx_response() */
280 NULL, /* on_tsx_state() */
281
282};
283
284
285/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000286 * Another simple module to handle incoming OPTIONS request
287 */
288
289/* Notification on incoming request */
290static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
291{
292 pjsip_tx_data *tdata;
293 pjsip_response_addr res_addr;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000294 pjmedia_transport_info tpinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000295 pjmedia_sdp_session *sdp;
296 const pjsip_hdr *cap_hdr;
297 pj_status_t status;
298
299 /* Only want to handle OPTIONS requests */
300 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000301 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000302 {
303 return PJ_FALSE;
304 }
305
306 /* Create basic response. */
307 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
308 &tdata);
309 if (status != PJ_SUCCESS) {
310 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
311 return PJ_TRUE;
312 }
313
314 /* Add Allow header */
315 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
316 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000317 pjsip_msg_add_hdr(tdata->msg,
318 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000319 }
320
321 /* Add Accept header */
322 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
323 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000324 pjsip_msg_add_hdr(tdata->msg,
325 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000326 }
327
328 /* Add Supported header */
329 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
330 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000331 pjsip_msg_add_hdr(tdata->msg,
332 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000333 }
334
335 /* Add Allow-Events header from the evsub module */
336 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
337 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000338 pjsip_msg_add_hdr(tdata->msg,
339 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000340 }
341
342 /* Add User-Agent header */
343 if (pjsua_var.ua_cfg.user_agent.slen) {
344 const pj_str_t USER_AGENT = { "User-Agent", 10};
345 pjsip_hdr *h;
346
347 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
348 &USER_AGENT,
349 &pjsua_var.ua_cfg.user_agent);
350 pjsip_msg_add_hdr(tdata->msg, h);
351 }
352
Benny Prijonoa248b952009-08-12 22:28:47 +0000353 /* Get media socket info, make sure transport is ready */
354 if (pjsua_var.calls[0].med_tp) {
355 pjmedia_transport_info_init(&tpinfo);
356 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000357
Benny Prijonoa248b952009-08-12 22:28:47 +0000358 /* Add SDP body, using call0's RTP address */
359 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
360 &tpinfo.sock_info, &sdp);
361 if (status == PJ_SUCCESS) {
362 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
363 }
Benny Prijono56315612006-07-18 14:39:40 +0000364 }
365
366 /* Send response statelessly */
367 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
368 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
369 if (status != PJ_SUCCESS)
370 pjsip_tx_data_dec_ref(tdata);
371
372 return PJ_TRUE;
373}
374
375
376/* The module instance. */
377static pjsip_module pjsua_options_handler =
378{
379 NULL, NULL, /* prev, next. */
380 { "mod-pjsua-options", 17 }, /* Name. */
381 -1, /* Id */
382 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
383 NULL, /* load() */
384 NULL, /* start() */
385 NULL, /* stop() */
386 NULL, /* unload() */
387 &options_on_rx_request, /* on_rx_request() */
388 NULL, /* on_rx_response() */
389 NULL, /* on_tx_request. */
390 NULL, /* on_tx_response() */
391 NULL, /* on_tsx_state() */
392
393};
394
395
396/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000397 * These two functions are the main callbacks registered to PJSIP stack
398 * to receive SIP request and response messages that are outside any
399 * dialogs and any transactions.
400 */
Benny Prijono268ca612006-02-07 12:34:11 +0000401
402/*
403 * Handler for receiving incoming requests.
404 *
405 * This handler serves multiple purposes:
406 * - it receives requests outside dialogs.
407 * - it receives requests inside dialogs, when the requests are
408 * unhandled by other dialog usages. Example of these
409 * requests are: MESSAGE.
410 */
411static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
412{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000413 pj_bool_t processed = PJ_FALSE;
414
415 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000416
Benny Prijono84126ab2006-02-09 09:30:09 +0000417 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000418
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000419 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000420 }
421
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000422 PJSUA_UNLOCK();
423
424 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000425}
426
427
428/*
429 * Handler for receiving incoming responses.
430 *
431 * This handler serves multiple purposes:
432 * - it receives strayed responses (i.e. outside any dialog and
433 * outside any transactions).
434 * - it receives responses coming to a transaction, when pjsua
435 * module is set as transaction user for the transaction.
436 * - it receives responses inside a dialog, when these responses
437 * are unhandled by other dialog usages.
438 */
439static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
440{
441 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000442 return PJ_FALSE;
443}
444
445
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000446/*****************************************************************************
447 * Logging.
448 */
449
450/* Log callback */
451static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000452{
Benny Prijono572d4852006-11-23 21:50:02 +0000453 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000454
455 if (pjsua_var.log_file) {
456 pj_ssize_t size = len;
457 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000458 /* This will slow things down considerably! Don't do it!
459 pj_file_flush(pjsua_var.log_file);
460 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000461 }
462
Benny Prijono572d4852006-11-23 21:50:02 +0000463 if (level <= (int)pjsua_var.log_cfg.console_level) {
464 if (pjsua_var.log_cfg.cb)
465 (*pjsua_var.log_cfg.cb)(level, buffer, len);
466 else
467 pj_log_write(level, buffer, len);
468 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000469}
470
471
472/*
473 * Application can call this function at any time (after pjsua_create(), of
474 * course) to change logging settings.
475 */
476PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
477{
478 pj_status_t status;
479
480 /* Save config. */
481 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
482
483 /* Redirect log function to ours */
484 pj_log_set_log_func( &log_writer );
485
Benny Prijono9cb09a22007-08-30 09:35:10 +0000486 /* Set decor */
487 pj_log_set_decor(pjsua_var.log_cfg.decor);
488
Benny Prijono4190cf92008-01-18 13:25:05 +0000489 /* Set log level */
490 pj_log_set_level(pjsua_var.log_cfg.level);
491
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 /* Close existing file, if any */
493 if (pjsua_var.log_file) {
494 pj_file_close(pjsua_var.log_file);
495 pjsua_var.log_file = NULL;
496 }
497
498 /* If output log file is desired, create the file: */
499 if (pjsua_var.log_cfg.log_filename.slen) {
Benny Prijonodbe3f4b2009-05-07 16:56:04 +0000500 unsigned flags = PJ_O_WRONLY;
501 flags |= pjsua_var.log_cfg.log_file_flags;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000502 status = pj_file_open(pjsua_var.pool,
503 pjsua_var.log_cfg.log_filename.ptr,
Benny Prijonodbe3f4b2009-05-07 16:56:04 +0000504 flags,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 &pjsua_var.log_file);
506
507 if (status != PJ_SUCCESS) {
508 pjsua_perror(THIS_FILE, "Error creating log file", status);
509 return status;
510 }
511 }
512
513 /* Unregister msg logging if it's previously registered */
514 if (pjsua_msg_logger.id >= 0) {
515 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
516 pjsua_msg_logger.id = -1;
517 }
518
519 /* Enable SIP message logging */
520 if (pjsua_var.log_cfg.msg_logging)
521 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
522
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000523 return PJ_SUCCESS;
524}
525
526
527/*****************************************************************************
528 * PJSUA Base API.
529 */
530
531/* Worker thread function. */
532static int worker_thread(void *arg)
533{
534 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000535
Benny Prijono268ca612006-02-07 12:34:11 +0000536 PJ_UNUSED_ARG(arg);
537
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 while (!pjsua_var.thread_quit_flag) {
539 int count;
540
541 count = pjsua_handle_events(TIMEOUT);
542 if (count < 0)
543 pj_thread_sleep(TIMEOUT);
544 }
Benny Prijono268ca612006-02-07 12:34:11 +0000545
546 return 0;
547}
548
Benny Prijonodc39fe82006-05-26 12:17:46 +0000549
Benny Prijono8389c312008-02-21 21:36:34 +0000550/* Init random seed */
551static void init_random_seed(void)
552{
553 pj_sockaddr addr;
554 const pj_str_t *hostname;
555 pj_uint32_t pid;
556 pj_time_val t;
557 unsigned seed=0;
558
559 /* Add hostname */
560 hostname = pj_gethostname();
561 seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen);
562
563 /* Add primary IP address */
564 if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS)
565 seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4);
566
567 /* Get timeofday */
568 pj_gettimeofday(&t);
569 seed = pj_hash_calc(seed, &t, sizeof(t));
570
571 /* Add PID */
572 pid = pj_getpid();
573 seed = pj_hash_calc(seed, &pid, sizeof(pid));
574
575 /* Init random seed */
576 pj_srand(seed);
577}
578
Benny Prijono268ca612006-02-07 12:34:11 +0000579/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000581 */
582PJ_DEF(pj_status_t) pjsua_create(void)
583{
584 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000585
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000586 /* Init pjsua data */
587 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000588
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000589 /* Set default logging settings */
590 pjsua_logging_config_default(&pjsua_var.log_cfg);
591
592 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000593 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
595
Benny Prijono8389c312008-02-21 21:36:34 +0000596 /* Init random seed */
597 init_random_seed();
Benny Prijono268ca612006-02-07 12:34:11 +0000598
Benny Prijonofccab712006-02-22 22:23:22 +0000599 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000600 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000601 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000602
Benny Prijonoec921342007-03-24 13:00:30 +0000603 /* Init PJNATH */
604 status = pjnath_init();
605 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000606
Benny Prijono094d3ad2006-11-21 08:41:00 +0000607 /* Set default sound device ID */
Benny Prijono96e74f32009-02-22 12:00:12 +0000608 pjsua_var.cap_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
609 pjsua_var.play_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
Benny Prijono094d3ad2006-11-21 08:41:00 +0000610
Benny Prijono268ca612006-02-07 12:34:11 +0000611 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000612 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000613
614 /* Create memory pool for application. */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000615 pjsua_var.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000616
617 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000618
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000619 /* Create mutex */
620 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
621 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000622 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000623 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000624 return status;
625 }
626
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000627 /* Must create SIP endpoint to initialize SIP parser. The parser
628 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000629 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000630 status = pjsip_endpt_create(&pjsua_var.cp.factory,
631 pj_gethostname()->ptr,
632 &pjsua_var.endpt);
633 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000634
635
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000636 return PJ_SUCCESS;
637}
638
639
640/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641 * Initialize pjsua with the specified settings. All the settings are
642 * optional, and the default values will be used when the config is not
643 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000644 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000645PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
646 const pjsua_logging_config *log_cfg,
647 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000648{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000649 pjsua_config default_cfg;
650 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000651 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000652 pjsip_ua_init_param ua_init_param;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000653 pj_status_t status;
654
655
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000656 /* Create default configurations when the config is not supplied */
657
658 if (ua_cfg == NULL) {
659 pjsua_config_default(&default_cfg);
660 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000661 }
662
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000663 if (media_cfg == NULL) {
664 pjsua_media_config_default(&default_media_cfg);
665 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000666 }
667
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000668 /* Initialize logging first so that info/errors can be captured */
669 if (log_cfg) {
670 status = pjsua_reconfigure_logging(log_cfg);
Benny Prijono329d6382009-05-29 13:04:03 +0000671 if (status != PJ_SUCCESS)
672 return status;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000673 }
674
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000675 /* If nameserver is configured, create DNS resolver instance and
676 * set it to be used by SIP resolver.
677 */
678 if (ua_cfg->nameserver_count) {
679#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000680 unsigned i;
681
682 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000683 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
684 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000685 if (status != PJ_SUCCESS) {
686 pjsua_perror(THIS_FILE, "Error creating resolver", status);
687 return status;
688 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000689
690 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000691 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
692 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000693 ua_cfg->nameserver, NULL);
694 if (status != PJ_SUCCESS) {
695 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
696 return status;
697 }
698
699 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000700 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000701 if (status != PJ_SUCCESS) {
702 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
703 return status;
704 }
705
706 /* Print nameservers */
707 for (i=0; i<ua_cfg->nameserver_count; ++i) {
708 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
709 (int)ua_cfg->nameserver[i].slen,
710 ua_cfg->nameserver[i].ptr));
711 }
712#else
713 PJ_LOG(2,(THIS_FILE,
714 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
715#endif
716 }
717
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000718 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000719
720 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
722 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000723
Benny Prijonodc39fe82006-05-26 12:17:46 +0000724
725 /* Initialize UA layer module: */
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000726 pj_bzero(&ua_init_param, sizeof(ua_init_param));
727 if (ua_cfg->hangup_forked_call) {
728 ua_init_param.on_dlg_forked = &on_dlg_forked;
729 }
730 status = pjsip_ua_init_module( pjsua_var.endpt, &ua_init_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000731 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000732
Benny Prijonodc39fe82006-05-26 12:17:46 +0000733
Benny Prijono053f5222006-11-11 16:16:04 +0000734 /* Initialize Replaces support. */
735 status = pjsip_replaces_init_module( pjsua_var.endpt );
736 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
737
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000738 /* Initialize 100rel support */
739 status = pjsip_100rel_init_module(pjsua_var.endpt);
740 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000741
Nanang Izzuddin59dffb12009-08-11 12:42:38 +0000742 /* Initialize session timer support */
743 status = pjsip_timer_init_module(pjsua_var.endpt);
744 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
745
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000746 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000747 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000748 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000749 {
750 NULL, NULL, /* prev, next. */
751 { "mod-pjsua", 9 }, /* Name. */
752 -1, /* Id */
753 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
754 NULL, /* load() */
755 NULL, /* start() */
756 NULL, /* stop() */
757 NULL, /* unload() */
758 &mod_pjsua_on_rx_request, /* on_rx_request() */
759 &mod_pjsua_on_rx_response, /* on_rx_response() */
760 NULL, /* on_tx_request. */
761 NULL, /* on_tx_response() */
762 NULL, /* on_tsx_state() */
763 };
764
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000765 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000766
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000767 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
768 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000769 }
770
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000771
Benny Prijonodc39fe82006-05-26 12:17:46 +0000772
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000773 /* Initialize PJSUA call subsystem: */
774 status = pjsua_call_subsys_init(ua_cfg);
775 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000776 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000777
Benny Prijonobb995fd2009-08-12 11:03:23 +0000778 /* Convert deprecated STUN settings */
779 if (pjsua_var.ua_cfg.stun_srv_cnt==0) {
780 if (pjsua_var.ua_cfg.stun_domain.slen) {
781 pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] =
782 pjsua_var.ua_cfg.stun_domain;
783 }
784 if (pjsua_var.ua_cfg.stun_host.slen) {
785 pjsua_var.ua_cfg.stun_srv[pjsua_var.ua_cfg.stun_srv_cnt++] =
786 pjsua_var.ua_cfg.stun_host;
787 }
788 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000789
Benny Prijonoc97608e2007-03-23 16:34:20 +0000790 /* Start resolving STUN server */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000791 status = resolve_stun_server(PJ_FALSE);
Benny Prijonoc97608e2007-03-23 16:34:20 +0000792 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
793 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
794 return status;
795 }
796
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000797 /* Initialize PJSUA media subsystem */
798 status = pjsua_media_subsys_init(media_cfg);
799 if (status != PJ_SUCCESS)
800 goto on_error;
801
Benny Prijonodc39fe82006-05-26 12:17:46 +0000802
803 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000804 status = pjsip_evsub_init_module(pjsua_var.endpt);
805 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000806
Benny Prijonodc39fe82006-05-26 12:17:46 +0000807
808 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000809 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
810 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000811
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000812 /* Init PUBLISH module */
813 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000814
815 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000816 status = pjsip_xfer_init_module( pjsua_var.endpt );
817 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000818
819 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000820 status = pjsua_pres_init();
821 if (status != PJ_SUCCESS)
822 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000823
824 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000825 status = pjsua_im_init();
826 if (status != PJ_SUCCESS)
827 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000828
Benny Prijonoc8141a82006-08-20 09:12:19 +0000829 /* Register OPTIONS handler */
830 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
831
832 /* Add OPTIONS in Allow header */
833 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
834 NULL, 1, &STR_OPTIONS);
835
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000836 /* Start worker thread if needed. */
837 if (pjsua_var.ua_cfg.thread_cnt) {
838 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000839
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000840 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
841 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000842
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000843 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
844 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
845 NULL, 0, 0, &pjsua_var.thread[i]);
846 if (status != PJ_SUCCESS)
847 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000848 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000849 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
850 pjsua_var.ua_cfg.thread_cnt));
851 } else {
852 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000853 }
854
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000855 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000856
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000857 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000858 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000859
Benny Prijono268ca612006-02-07 12:34:11 +0000860 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000861
862on_error:
863 pjsua_destroy();
864 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000865}
866
867
Benny Prijono834aee32006-02-19 01:38:06 +0000868/* Sleep with polling */
869static void busy_sleep(unsigned msec)
870{
871 pj_time_val timeout, now;
872
873 pj_gettimeofday(&timeout);
874 timeout.msec += msec;
875 pj_time_val_normalize(&timeout);
876
877 do {
Nanang Izzuddin90b83202009-03-02 15:48:45 +0000878 int i;
879 i = msec / 10;
880 while (pjsua_handle_events(10) > 0 && i > 0)
881 --i;
Benny Prijono834aee32006-02-19 01:38:06 +0000882 pj_gettimeofday(&now);
883 } while (PJ_TIME_VAL_LT(now, timeout));
884}
885
Benny Prijonobb995fd2009-08-12 11:03:23 +0000886/* Internal function to destroy STUN resolution session
887 * (pj_stun_resolve).
Benny Prijonoebbf6892007-03-24 17:37:25 +0000888 */
Benny Prijonobb995fd2009-08-12 11:03:23 +0000889static void destroy_stun_resolve(pjsua_stun_resolve *sess)
Benny Prijonoebbf6892007-03-24 17:37:25 +0000890{
Benny Prijonobb995fd2009-08-12 11:03:23 +0000891 PJSUA_LOCK();
892 pj_list_erase(sess);
893 PJSUA_UNLOCK();
Benny Prijonoebbf6892007-03-24 17:37:25 +0000894
Benny Prijonobb995fd2009-08-12 11:03:23 +0000895 pj_assert(sess->stun_sock==NULL);
896 pj_pool_release(sess->pool);
897}
Benny Prijonoebbf6892007-03-24 17:37:25 +0000898
Benny Prijonobb995fd2009-08-12 11:03:23 +0000899/* This is the internal function to be called when STUN resolution
900 * session (pj_stun_resolve) has completed.
901 */
902static void stun_resolve_complete(pjsua_stun_resolve *sess)
903{
904 pj_stun_resolve_result result;
905
906 pj_bzero(&result, sizeof(result));
907 result.token = sess->token;
908 result.status = sess->status;
909 result.name = sess->srv[sess->idx];
910 pj_memcpy(&result.addr, &sess->addr, sizeof(result.addr));
911
912 if (result.status == PJ_SUCCESS) {
913 char addr[PJ_INET6_ADDRSTRLEN+10];
914 pj_sockaddr_print(&result.addr, addr, sizeof(addr), 3);
915 PJ_LOG(4,(THIS_FILE,
916 "STUN resolution success, using %.*s, address is %s",
917 (int)sess->srv[sess->idx].slen,
918 sess->srv[sess->idx].ptr,
919 addr));
920 } else {
921 char errmsg[PJ_ERR_MSG_SIZE];
922 pj_strerror(result.status, errmsg, sizeof(errmsg));
923 PJ_LOG(1,(THIS_FILE, "STUN resolution failed: %s", errmsg));
924 }
925
926 sess->cb(&result);
927
928 if (!sess->blocking) {
929 destroy_stun_resolve(sess);
930 }
931}
932
933/* This is the callback called by the STUN socket (pj_stun_sock)
934 * to report it's state. We use this as part of testing the
935 * STUN server.
936 */
937static pj_bool_t test_stun_on_status(pj_stun_sock *stun_sock,
938 pj_stun_sock_op op,
939 pj_status_t status)
940{
941 pjsua_stun_resolve *sess;
942
Nanang Izzuddinae1c6152009-08-17 16:30:04 +0000943 sess = (pjsua_stun_resolve*) pj_stun_sock_get_user_data(stun_sock);
Benny Prijonobb995fd2009-08-12 11:03:23 +0000944 pj_assert(stun_sock == sess->stun_sock);
945
Benny Prijonoebbf6892007-03-24 17:37:25 +0000946 if (status != PJ_SUCCESS) {
Benny Prijonobb995fd2009-08-12 11:03:23 +0000947 char errmsg[PJ_ERR_MSG_SIZE];
948 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000949
Benny Prijonobb995fd2009-08-12 11:03:23 +0000950 PJ_LOG(4,(THIS_FILE, "STUN resolution for %.*s failed: %s",
951 (int)sess->srv[sess->idx].slen,
952 sess->srv[sess->idx].ptr, errmsg));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000953
Benny Prijonobb995fd2009-08-12 11:03:23 +0000954 sess->status = status;
955
956 pj_stun_sock_destroy(stun_sock);
957 sess->stun_sock = NULL;
958
959 ++sess->idx;
960 resolve_stun_entry(sess);
961
962 return PJ_FALSE;
963
964 } else if (op == PJ_STUN_SOCK_BINDING_OP) {
965 pj_stun_sock_info ssi;
966
967 pj_stun_sock_get_info(stun_sock, &ssi);
968 pj_memcpy(&sess->addr, &ssi.srv_addr, sizeof(sess->addr));
969
970 sess->status = PJ_SUCCESS;
971 pj_stun_sock_destroy(stun_sock);
972 sess->stun_sock = NULL;
973
974 stun_resolve_complete(sess);
975
976 return PJ_FALSE;
977
978 } else
979 return PJ_TRUE;
980
981}
982
983/* This is an internal function to resolve and test current
984 * server entry in pj_stun_resolve session. It is called by
985 * pjsua_resolve_stun_servers() and test_stun_on_status() above
986 */
987static void resolve_stun_entry(pjsua_stun_resolve *sess)
988{
989 /* Loop while we have entry to try */
990 for (; sess->idx < sess->count; ++sess->idx) {
991 const int af = pj_AF_INET();
992 pj_str_t hostpart;
993 pj_uint16_t port;
994 pj_stun_sock_cb stun_sock_cb;
995
996 pj_assert(sess->idx < sess->count);
997
998 /* Parse the server entry into host:port */
999 sess->status = pj_sockaddr_parse2(af, 0, &sess->srv[sess->idx],
1000 &hostpart, &port, NULL);
1001 if (sess->status != PJ_SUCCESS) {
1002 PJ_LOG(2,(THIS_FILE, "Invalid STUN server entry %.*s",
1003 (int)sess->srv[sess->idx].slen,
1004 sess->srv[sess->idx].ptr));
1005 continue;
Benny Prijonoebbf6892007-03-24 17:37:25 +00001006 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001007
1008 /* Use default port if not specified */
1009 if (port == 0)
1010 port = PJ_STUN_PORT;
1011
1012 pj_assert(sess->stun_sock == NULL);
1013
1014 PJ_LOG(4,(THIS_FILE, "Trying STUN server %.*s (%d of %d)..",
1015 (int)sess->srv[sess->idx].slen,
1016 sess->srv[sess->idx].ptr,
1017 sess->idx+1, sess->count));
1018
1019 /* Use STUN_sock to test this entry */
1020 pj_bzero(&stun_sock_cb, sizeof(stun_sock_cb));
1021 stun_sock_cb.on_status = &test_stun_on_status;
1022 sess->status = pj_stun_sock_create(&pjsua_var.stun_cfg, "stunresolve",
1023 pj_AF_INET(), &stun_sock_cb,
1024 NULL, sess, &sess->stun_sock);
1025 if (sess->status != PJ_SUCCESS) {
1026 char errmsg[PJ_ERR_MSG_SIZE];
1027 pj_strerror(sess->status, errmsg, sizeof(errmsg));
1028 PJ_LOG(4,(THIS_FILE,
1029 "Error creating STUN socket for %.*s: %s",
1030 (int)sess->srv[sess->idx].slen,
1031 sess->srv[sess->idx].ptr, errmsg));
1032
1033 continue;
1034 }
1035
1036 sess->status = pj_stun_sock_start(sess->stun_sock, &hostpart,
1037 port, pjsua_var.resolver);
1038 if (sess->status != PJ_SUCCESS) {
1039 char errmsg[PJ_ERR_MSG_SIZE];
1040 pj_strerror(sess->status, errmsg, sizeof(errmsg));
1041 PJ_LOG(4,(THIS_FILE,
1042 "Error starting STUN socket for %.*s: %s",
1043 (int)sess->srv[sess->idx].slen,
1044 sess->srv[sess->idx].ptr, errmsg));
1045
1046 pj_stun_sock_destroy(sess->stun_sock);
1047 sess->stun_sock = NULL;
1048 continue;
1049 }
1050
1051 /* Done for now, testing will resume/complete asynchronously in
1052 * stun_sock_cb()
1053 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001054 return;
1055 }
1056
Benny Prijonobb995fd2009-08-12 11:03:23 +00001057 if (sess->idx >= sess->count) {
1058 /* No more entries to try */
1059 PJ_ASSERT_ON_FAIL(sess->status != PJ_SUCCESS,
1060 sess->status = PJ_EUNKNOWN);
1061 stun_resolve_complete(sess);
1062 }
1063}
Benny Prijonoebbf6892007-03-24 17:37:25 +00001064
Benny Prijonoebbf6892007-03-24 17:37:25 +00001065
Benny Prijonobb995fd2009-08-12 11:03:23 +00001066/*
1067 * Resolve STUN server.
1068 */
1069PJ_DEF(pj_status_t) pjsua_resolve_stun_servers( unsigned count,
1070 pj_str_t srv[],
1071 pj_bool_t wait,
1072 void *token,
1073 pj_stun_resolve_cb cb)
1074{
1075 pj_pool_t *pool;
1076 pjsua_stun_resolve *sess;
1077 pj_status_t status;
1078 unsigned i;
1079
1080 PJ_ASSERT_RETURN(count && srv && cb, PJ_EINVAL);
1081
1082 pool = pjsua_pool_create("stunres", 256, 256);
1083 if (!pool)
1084 return PJ_ENOMEM;
1085
1086 sess = PJ_POOL_ZALLOC_T(pool, pjsua_stun_resolve);
1087 sess->pool = pool;
1088 sess->token = token;
1089 sess->cb = cb;
1090 sess->count = count;
1091 sess->blocking = wait;
1092 sess->status = PJ_EPENDING;
1093 sess->srv = (pj_str_t*) pj_pool_calloc(pool, count, sizeof(pj_str_t));
1094 for (i=0; i<count; ++i) {
1095 pj_strdup(pool, &sess->srv[i], &srv[i]);
Benny Prijonoebbf6892007-03-24 17:37:25 +00001096 }
1097
Benny Prijonobb995fd2009-08-12 11:03:23 +00001098 PJSUA_LOCK();
1099 pj_list_push_back(&pjsua_var.stun_res, sess);
1100 PJSUA_UNLOCK();
1101
1102 resolve_stun_entry(sess);
1103
1104 if (!wait)
1105 return PJ_SUCCESS;
1106
1107 while (sess->status == PJ_EPENDING) {
1108 pjsua_handle_events(50);
1109 }
1110
1111 status = sess->status;
1112 destroy_stun_resolve(sess);
1113
1114 return status;
1115}
1116
1117/*
1118 * Cancel pending STUN resolution.
1119 */
1120PJ_DEF(pj_status_t) pjsua_cancel_stun_resolution( void *token,
1121 pj_bool_t notify_cb)
1122{
1123 pjsua_stun_resolve *sess;
1124 unsigned cancelled_count = 0;
1125
1126 PJSUA_LOCK();
1127 sess = pjsua_var.stun_res.next;
1128 while (sess != &pjsua_var.stun_res) {
1129 pjsua_stun_resolve *next = sess->next;
1130
1131 if (sess->token == token) {
1132 if (notify_cb) {
1133 pj_stun_resolve_result result;
1134
1135 pj_bzero(&result, sizeof(result));
1136 result.token = token;
1137 result.status = PJ_ECANCELLED;
1138
1139 sess->cb(&result);
1140 }
1141
1142 destroy_stun_resolve(sess);
1143 ++cancelled_count;
1144 }
1145
1146 sess = next;
1147 }
1148 PJSUA_UNLOCK();
1149
1150 return cancelled_count ? PJ_SUCCESS : PJ_ENOTFOUND;
1151}
1152
1153static void internal_stun_resolve_cb(const pj_stun_resolve_result *result)
1154{
1155 pjsua_var.stun_status = result->status;
1156 if (result->status == PJ_SUCCESS) {
1157 pj_memcpy(&pjsua_var.stun_srv, &result->addr, sizeof(result->addr));
1158 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001159}
1160
Benny Prijono268ca612006-02-07 12:34:11 +00001161/*
Benny Prijonoc97608e2007-03-23 16:34:20 +00001162 * Resolve STUN server.
1163 */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001164pj_status_t resolve_stun_server(pj_bool_t wait)
Benny Prijonoc97608e2007-03-23 16:34:20 +00001165{
1166 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
Benny Prijonobb995fd2009-08-12 11:03:23 +00001167 pj_status_t status;
1168
Benny Prijonoc97608e2007-03-23 16:34:20 +00001169 /* Initialize STUN configuration */
1170 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
1171 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
1172 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
1173
1174 /* Start STUN server resolution */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001175 if (pjsua_var.ua_cfg.stun_srv_cnt) {
1176 pjsua_var.stun_status = PJ_EPENDING;
1177 status = pjsua_resolve_stun_servers(pjsua_var.ua_cfg.stun_srv_cnt,
1178 pjsua_var.ua_cfg.stun_srv,
1179 wait, NULL,
1180 &internal_stun_resolve_cb);
1181 if (wait || status != PJ_SUCCESS) {
Benny Prijonoda9785b2007-04-02 20:43:06 +00001182 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +00001183 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001184 } else {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001185 pjsua_var.stun_status = PJ_SUCCESS;
1186 }
1187
Benny Prijonoc97608e2007-03-23 16:34:20 +00001188 } else if (pjsua_var.stun_status == PJ_EPENDING) {
1189 /* STUN server resolution has been started, wait for the
1190 * result.
1191 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001192 if (wait) {
1193 while (pjsua_var.stun_status == PJ_EPENDING)
1194 pjsua_handle_events(10);
1195 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001196 }
Benny Prijonobb995fd2009-08-12 11:03:23 +00001197
1198 if (pjsua_var.stun_status != PJ_EPENDING &&
1199 pjsua_var.stun_status != PJ_SUCCESS &&
1200 pjsua_var.ua_cfg.stun_ignore_failure)
1201 {
1202 PJ_LOG(2,(THIS_FILE,
1203 "Ignoring STUN resolution failure (by setting)"));
1204 pjsua_var.stun_status = PJ_SUCCESS;
1205 }
1206
1207 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001208}
1209
1210/*
Benny Prijono268ca612006-02-07 12:34:11 +00001211 * Destroy pjsua.
1212 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001213PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001214{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001215 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001216
1217 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001218 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001219
1220 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001221 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1222 if (pjsua_var.thread[i]) {
1223 pj_thread_join(pjsua_var.thread[i]);
1224 pj_thread_destroy(pjsua_var.thread[i]);
1225 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001226 }
Benny Prijono268ca612006-02-07 12:34:11 +00001227 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001228
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001229 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001230 /* Terminate all calls. */
1231 pjsua_call_hangup_all();
1232
Benny Prijono7f6ee022008-07-31 08:32:46 +00001233 /* Set all accounts to offline */
1234 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1235 if (!pjsua_var.acc[i].valid)
1236 continue;
1237 pjsua_var.acc[i].online_status = PJ_FALSE;
1238 pj_bzero(&pjsua_var.acc[i].rpid, sizeof(pjrpid_element));
1239 }
1240
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001241 /* Terminate all presence subscriptions. */
1242 pjsua_pres_shutdown();
1243
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001244 /* Unregister all accounts */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001245 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1246 if (!pjsua_var.acc[i].valid)
1247 continue;
1248
1249 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001250 pjsua_acc_set_registration(i, PJ_FALSE);
1251 }
1252 }
Benny Prijono9fc735d2006-05-28 14:58:12 +00001253 }
Benny Prijono834aee32006-02-19 01:38:06 +00001254
Benny Prijono268ca612006-02-07 12:34:11 +00001255 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001256 if (pjsua_var.endpt) {
Benny Prijonobb995fd2009-08-12 11:03:23 +00001257
1258 /* Terminate any pending STUN resolution */
1259 if (!pj_list_empty(&pjsua_var.stun_res)) {
1260 pjsua_stun_resolve *sess = pjsua_var.stun_res.next;
1261 while (sess != &pjsua_var.stun_res) {
1262 pjsua_stun_resolve *next = sess->next;
1263 destroy_stun_resolve(sess);
1264 sess = next;
1265 }
1266 }
1267
Benny Prijonoff3b1462008-07-14 09:32:14 +00001268 /* Wait for some time to allow unregistration and ICE/TURN
1269 * transports shutdown to complete:
1270 */
1271 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1272 busy_sleep(1000);
1273
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001274 PJ_LOG(4,(THIS_FILE, "Destroying..."));
1275
Benny Prijonoa248b952009-08-12 22:28:47 +00001276 /* Terminate all calls again, just in case there's new call
1277 * picked up during busy_sleep()
1278 */
1279 pjsua_call_hangup_all();
1280
1281 /* Destroy media after all polling is done, as there may be
1282 * incoming request that needs handling (e.g. OPTIONS)
1283 */
1284 pjsua_media_subsys_destroy();
1285
Benny Prijonod7e26582008-07-18 23:51:49 +00001286 /* Must destroy endpoint first before destroying pools in
1287 * buddies or accounts, since shutting down transaction layer
1288 * may emit events which trigger some buddy or account callbacks
1289 * to be called.
1290 */
1291 pjsip_endpt_destroy(pjsua_var.endpt);
1292 pjsua_var.endpt = NULL;
1293
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001294 /* Destroy pool in the buddy object */
1295 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1296 if (pjsua_var.buddy[i].pool) {
1297 pj_pool_release(pjsua_var.buddy[i].pool);
1298 pjsua_var.buddy[i].pool = NULL;
1299 }
1300 }
1301
1302 /* Destroy accounts */
1303 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1304 if (pjsua_var.acc[i].pool) {
1305 pj_pool_release(pjsua_var.acc[i].pool);
1306 pjsua_var.acc[i].pool = NULL;
1307 }
1308 }
Benny Prijonoa248b952009-08-12 22:28:47 +00001309 } else {
1310 /* Destroy media */
1311 pjsua_media_subsys_destroy();
Benny Prijono9fc735d2006-05-28 14:58:12 +00001312 }
Benny Prijono268ca612006-02-07 12:34:11 +00001313
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001314 /* Destroy mutex */
1315 if (pjsua_var.mutex) {
1316 pj_mutex_destroy(pjsua_var.mutex);
1317 pjsua_var.mutex = NULL;
1318 }
1319
1320 /* Destroy pool and pool factory. */
1321 if (pjsua_var.pool) {
1322 pj_pool_release(pjsua_var.pool);
1323 pjsua_var.pool = NULL;
1324 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001325
1326 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1327
1328 /* End logging */
1329 if (pjsua_var.log_file) {
1330 pj_file_close(pjsua_var.log_file);
1331 pjsua_var.log_file = NULL;
1332 }
1333
1334 /* Shutdown PJLIB */
1335 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001336 }
Benny Prijono268ca612006-02-07 12:34:11 +00001337
Benny Prijonof762ee72006-12-01 11:14:37 +00001338 /* Clear pjsua_var */
1339 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1340
Benny Prijono268ca612006-02-07 12:34:11 +00001341 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001342 return PJ_SUCCESS;
1343}
1344
1345
1346/**
1347 * Application is recommended to call this function after all initialization
1348 * is done, so that the library can do additional checking set up
1349 * additional
1350 *
1351 * @return PJ_SUCCESS on success, or the appropriate error code.
1352 */
1353PJ_DEF(pj_status_t) pjsua_start(void)
1354{
1355 pj_status_t status;
1356
1357 status = pjsua_call_subsys_start();
1358 if (status != PJ_SUCCESS)
1359 return status;
1360
1361 status = pjsua_media_subsys_start();
1362 if (status != PJ_SUCCESS)
1363 return status;
1364
1365 status = pjsua_pres_start();
1366 if (status != PJ_SUCCESS)
1367 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001368
1369 return PJ_SUCCESS;
1370}
1371
Benny Prijono9fc735d2006-05-28 14:58:12 +00001372
1373/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001374 * Poll pjsua for events, and if necessary block the caller thread for
1375 * the specified maximum interval (in miliseconds).
1376 */
1377PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1378{
Benny Prijono897f9f82007-05-03 19:56:21 +00001379#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001380
1381 return pj_symbianos_poll(-1, msec_timeout);
1382
Benny Prijono897f9f82007-05-03 19:56:21 +00001383#else
1384
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001385 unsigned count = 0;
1386 pj_time_val tv;
1387 pj_status_t status;
1388
1389 tv.sec = 0;
1390 tv.msec = msec_timeout;
1391 pj_time_val_normalize(&tv);
1392
1393 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1394
1395 if (status != PJ_SUCCESS)
1396 return -status;
1397
1398 return count;
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001399
Benny Prijono897f9f82007-05-03 19:56:21 +00001400#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001401}
1402
1403
1404/*
1405 * Create memory pool.
1406 */
1407PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1408 pj_size_t increment)
1409{
1410 /* Pool factory is thread safe, no need to lock */
1411 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1412 NULL);
1413}
1414
1415
1416/*
1417 * Internal function to get SIP endpoint instance of pjsua, which is
1418 * needed for example to register module, create transports, etc.
1419 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001420 */
1421PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1422{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001423 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001424}
1425
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001426/*
1427 * Internal function to get media endpoint instance.
1428 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001429 */
1430PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1431{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001432 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001433}
1434
Benny Prijono97b87172006-08-24 14:25:14 +00001435/*
1436 * Internal function to get PJSUA pool factory.
1437 */
1438PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1439{
1440 return &pjsua_var.cp.factory;
1441}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442
1443/*****************************************************************************
1444 * PJSUA SIP Transport API.
1445 */
1446
1447/*
Benny Prijono23674a32007-12-01 08:59:25 +00001448 * Tools to get address string.
1449 */
1450static const char *addr_string(const pj_sockaddr_t *addr)
1451{
1452 static char str[128];
1453 str[0] = '\0';
1454 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1455 pj_sockaddr_get_addr(addr),
1456 str, sizeof(str));
1457 return str;
1458}
1459
1460/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001461 * Create and initialize SIP socket (and possibly resolve public
1462 * address via STUN, depending on config).
1463 */
Benny Prijono23674a32007-12-01 08:59:25 +00001464static pj_status_t create_sip_udp_sock(int af,
1465 const pj_str_t *bind_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001466 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001467 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001468 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001469{
Benny Prijono23674a32007-12-01 08:59:25 +00001470 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijonoc97608e2007-03-23 16:34:20 +00001471 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001472 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001473 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001474 pj_status_t status;
1475
Benny Prijonoc97608e2007-03-23 16:34:20 +00001476 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +00001477 status = resolve_stun_server(PJ_TRUE);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001478 if (status != PJ_SUCCESS) {
1479 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1480 return status;
1481 }
1482
Benny Prijono23674a32007-12-01 08:59:25 +00001483 /* Initialize bound address */
1484 if (bind_param->slen) {
1485 status = pj_sockaddr_init(af, &bind_addr, bind_param,
1486 (pj_uint16_t)port);
1487 if (status != PJ_SUCCESS) {
1488 pjsua_perror(THIS_FILE,
1489 "Unable to resolve transport bound address",
1490 status);
1491 return status;
1492 }
1493 } else {
1494 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1495 }
1496
1497 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001498 if (status != PJ_SUCCESS) {
1499 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001500 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001501 }
1502
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001503 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001504 if (status != PJ_SUCCESS) {
1505 pjsua_perror(THIS_FILE, "bind() error", status);
1506 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001507 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001508 }
1509
Benny Prijonoe347cb02007-02-14 14:36:13 +00001510 /* If port is zero, get the bound port */
1511 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001512 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001513 int namelen = sizeof(bound_addr);
1514 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1515 if (status != PJ_SUCCESS) {
1516 pjsua_perror(THIS_FILE, "getsockname() error", status);
1517 pj_sock_close(sock);
1518 return status;
1519 }
1520
Benny Prijono23674a32007-12-01 08:59:25 +00001521 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001522 }
1523
Benny Prijonoc97608e2007-03-23 16:34:20 +00001524 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001525 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1526 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001527 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001528 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001529 }
1530
1531 /* Get the published address, either by STUN or by resolving
1532 * the name of local host.
1533 */
Benny Prijono23674a32007-12-01 08:59:25 +00001534 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001535 /*
1536 * Public address is already specified, no need to resolve the
1537 * address, only set the port.
1538 */
Benny Prijono23674a32007-12-01 08:59:25 +00001539 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1540 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001541
1542 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001543 /*
1544 * STUN is specified, resolve the address with STUN.
1545 */
Benny Prijono23674a32007-12-01 08:59:25 +00001546 if (af != pj_AF_INET()) {
1547 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1548 pj_sock_close(sock);
1549 return PJ_EAFNOTSUP;
1550 }
1551
Benny Prijono14c2b862007-02-21 00:40:05 +00001552 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001553 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1554 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001555 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001556 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001557 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001558 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001559 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001560 }
1561
1562 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001563 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001564
Benny Prijono42d08d22007-12-20 11:23:07 +00001565 if (pj_sockaddr_has_addr(&bind_addr)) {
1566 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1567 } else {
1568 status = pj_gethostip(af, p_pub_addr);
1569 if (status != PJ_SUCCESS) {
1570 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1571 pj_sock_close(sock);
1572 return status;
1573 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001574 }
1575
Benny Prijono23674a32007-12-01 08:59:25 +00001576 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1577 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001578 }
1579
1580 *p_sock = sock;
1581
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001582 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001583 addr_string(p_pub_addr),
1584 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001585
Benny Prijonod8410532006-06-15 11:04:33 +00001586 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001587}
1588
1589
1590/*
1591 * Create SIP transport.
1592 */
1593PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1594 const pjsua_transport_config *cfg,
1595 pjsua_transport_id *p_id)
1596{
1597 pjsip_transport *tp;
1598 unsigned id;
1599 pj_status_t status;
1600
1601 PJSUA_LOCK();
1602
1603 /* Find empty transport slot */
1604 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001605 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001606 break;
1607 }
1608
1609 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1610 status = PJ_ETOOMANY;
1611 pjsua_perror(THIS_FILE, "Error creating transport", status);
1612 goto on_return;
1613 }
1614
1615 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001616 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001617 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001618 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001619 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001620 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001621 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001622 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001623 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001624 pjsip_host_port addr_name;
1625
1626 /* Supply default config if it's not specified */
1627 if (cfg == NULL) {
1628 pjsua_transport_config_default(&config);
1629 cfg = &config;
1630 }
1631
Benny Prijono0a5cad82006-09-26 13:21:02 +00001632 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001633 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1634 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001635 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001636 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1637 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001638 if (status != PJ_SUCCESS) {
1639 pjsua_perror(THIS_FILE,
1640 "Unable to resolve transport public address",
1641 status);
1642 goto on_return;
1643 }
1644 }
1645
1646 /* Create the socket and possibly resolve the address with STUN
1647 * (only when public address is not specified).
1648 */
Benny Prijono23674a32007-12-01 08:59:25 +00001649 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
1650 &cfg->bound_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001651 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001652 if (status != PJ_SUCCESS)
1653 goto on_return;
1654
Benny Prijono23674a32007-12-01 08:59:25 +00001655 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1656 addr_name.host = pj_str(hostbuf);
1657 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001658
1659 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001660 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1661 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001662 if (status != PJ_SUCCESS) {
1663 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1664 status);
1665 pj_sock_close(sock);
1666 goto on_return;
1667 }
1668
Benny Prijonoe93e2872006-06-28 16:46:49 +00001669
1670 /* Save the transport */
1671 pjsua_var.tpdata[id].type = type;
1672 pjsua_var.tpdata[id].local_name = tp->local_name;
1673 pjsua_var.tpdata[id].data.tp = tp;
1674
Benny Prijono3569c0d2007-04-06 10:29:20 +00001675#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1676
Benny Prijonob2477142007-12-05 04:09:59 +00001677 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001678 /*
1679 * Create TCP transport.
1680 */
1681 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001682 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001683 pjsip_tpfactory *tcp;
1684 pj_sockaddr_in local_addr;
1685
1686 /* Supply default config if it's not specified */
1687 if (cfg == NULL) {
1688 pjsua_transport_config_default(&config);
1689 cfg = &config;
1690 }
1691
1692 /* Init local address */
1693 pj_sockaddr_in_init(&local_addr, 0, 0);
1694
1695 if (cfg->port)
1696 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1697
Benny Prijono0a5cad82006-09-26 13:21:02 +00001698 if (cfg->bound_addr.slen) {
1699 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1700 if (status != PJ_SUCCESS) {
1701 pjsua_perror(THIS_FILE,
1702 "Unable to resolve transport bound address",
1703 status);
1704 goto on_return;
1705 }
1706 }
1707
1708 /* Init published name */
1709 pj_bzero(&a_name, sizeof(pjsip_host_port));
1710 if (cfg->public_addr.slen)
1711 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001712
1713 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001714 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1715 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001716
1717 if (status != PJ_SUCCESS) {
1718 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1719 status);
1720 goto on_return;
1721 }
1722
1723 /* Save the transport */
1724 pjsua_var.tpdata[id].type = type;
1725 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1726 pjsua_var.tpdata[id].data.factory = tcp;
1727
Benny Prijono3569c0d2007-04-06 10:29:20 +00001728#endif /* PJ_HAS_TCP */
1729
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001730#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1731 } else if (type == PJSIP_TRANSPORT_TLS) {
1732 /*
1733 * Create TLS transport.
1734 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001735 /*
1736 * Create TCP transport.
1737 */
1738 pjsua_transport_config config;
1739 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001740 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001741 pj_sockaddr_in local_addr;
1742
1743 /* Supply default config if it's not specified */
1744 if (cfg == NULL) {
1745 pjsua_transport_config_default(&config);
1746 config.port = 5061;
1747 cfg = &config;
1748 }
1749
1750 /* Init local address */
1751 pj_sockaddr_in_init(&local_addr, 0, 0);
1752
1753 if (cfg->port)
1754 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1755
1756 if (cfg->bound_addr.slen) {
1757 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1758 if (status != PJ_SUCCESS) {
1759 pjsua_perror(THIS_FILE,
1760 "Unable to resolve transport bound address",
1761 status);
1762 goto on_return;
1763 }
1764 }
1765
1766 /* Init published name */
1767 pj_bzero(&a_name, sizeof(pjsip_host_port));
1768 if (cfg->public_addr.slen)
1769 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001770
1771 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001772 &cfg->tls_setting,
1773 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001774 if (status != PJ_SUCCESS) {
1775 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1776 status);
1777 goto on_return;
1778 }
1779
1780 /* Save the transport */
1781 pjsua_var.tpdata[id].type = type;
1782 pjsua_var.tpdata[id].local_name = tls->addr_name;
1783 pjsua_var.tpdata[id].data.factory = tls;
1784#endif
1785
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001786 } else {
1787 status = PJSIP_EUNSUPTRANSPORT;
1788 pjsua_perror(THIS_FILE, "Error creating transport", status);
1789 goto on_return;
1790 }
1791
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001792
1793 /* Return the ID */
1794 if (p_id) *p_id = id;
1795
1796 status = PJ_SUCCESS;
1797
1798on_return:
1799
1800 PJSUA_UNLOCK();
1801
Benny Prijonod8410532006-06-15 11:04:33 +00001802 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001803}
1804
1805
1806/*
1807 * Register transport that has been created by application.
1808 */
1809PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1810 pjsua_transport_id *p_id)
1811{
1812 unsigned id;
1813
1814 PJSUA_LOCK();
1815
1816 /* Find empty transport slot */
1817 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001818 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001819 break;
1820 }
1821
1822 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1823 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1824 PJSUA_UNLOCK();
1825 return PJ_ETOOMANY;
1826 }
1827
1828 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001829 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001830 pjsua_var.tpdata[id].local_name = tp->local_name;
1831 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001832
1833 /* Return the ID */
1834 if (p_id) *p_id = id;
1835
1836 PJSUA_UNLOCK();
1837
1838 return PJ_SUCCESS;
1839}
1840
1841
1842/*
1843 * Enumerate all transports currently created in the system.
1844 */
1845PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1846 unsigned *p_count )
1847{
1848 unsigned i, count;
1849
1850 PJSUA_LOCK();
1851
1852 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1853 ++i)
1854 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001855 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001856 continue;
1857
1858 id[count++] = i;
1859 }
1860
1861 *p_count = count;
1862
1863 PJSUA_UNLOCK();
1864
1865 return PJ_SUCCESS;
1866}
1867
1868
1869/*
1870 * Get information about transports.
1871 */
1872PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1873 pjsua_transport_info *info)
1874{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001875 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001876 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001877
Benny Prijonoac623b32006-07-03 15:19:31 +00001878 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001879
1880 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001881 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1882 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001883
1884 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001885 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001886
1887 PJSUA_LOCK();
1888
Benny Prijonoe93e2872006-06-28 16:46:49 +00001889 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1890
1891 pjsip_transport *tp = t->data.tp;
1892
1893 if (tp == NULL) {
1894 PJSUA_UNLOCK();
1895 return PJ_EINVALIDOP;
1896 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001897
Benny Prijonoe93e2872006-06-28 16:46:49 +00001898 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001899 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001900 info->type_name = pj_str(tp->type_name);
1901 info->info = pj_str(tp->info);
1902 info->flag = tp->flag;
1903 info->addr_len = tp->addr_len;
1904 info->local_addr = tp->local_addr;
1905 info->local_name = tp->local_name;
1906 info->usage_count = pj_atomic_get(tp->ref_cnt);
1907
Benny Prijono0a5cad82006-09-26 13:21:02 +00001908 status = PJ_SUCCESS;
1909
Benny Prijonoe93e2872006-06-28 16:46:49 +00001910 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1911
1912 pjsip_tpfactory *factory = t->data.factory;
1913
1914 if (factory == NULL) {
1915 PJSUA_UNLOCK();
1916 return PJ_EINVALIDOP;
1917 }
1918
1919 info->id = id;
1920 info->type = t->type;
1921 info->type_name = pj_str("TCP");
1922 info->info = pj_str("TCP transport");
1923 info->flag = factory->flag;
1924 info->addr_len = sizeof(factory->local_addr);
1925 info->local_addr = factory->local_addr;
1926 info->local_name = factory->addr_name;
1927 info->usage_count = 0;
1928
Benny Prijono0a5cad82006-09-26 13:21:02 +00001929 status = PJ_SUCCESS;
1930
1931 } else {
1932 pj_assert(!"Unsupported transport");
1933 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001934 }
1935
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001936
1937 PJSUA_UNLOCK();
1938
Benny Prijono0a5cad82006-09-26 13:21:02 +00001939 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001940}
1941
1942
1943/*
1944 * Disable a transport or re-enable it.
1945 */
1946PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1947 pj_bool_t enabled)
1948{
1949 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001950 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1951 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001952
1953 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001954 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001955
1956
1957 /* To be done!! */
1958 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001959 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001960
1961 return PJ_EINVALIDOP;
1962}
1963
1964
1965/*
1966 * Close the transport.
1967 */
1968PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1969 pj_bool_t force )
1970{
Benny Prijono5ff61872007-02-01 03:37:11 +00001971 pj_status_t status;
1972
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001973 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001974 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1975 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001976
1977 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001978 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001979
Benny Prijono0a5cad82006-09-26 13:21:02 +00001980 /* Note: destroy() may not work if there are objects still referencing
1981 * the transport.
1982 */
1983 if (force) {
1984 switch (pjsua_var.tpdata[id].type) {
1985 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001986 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1987 if (status != PJ_SUCCESS)
1988 return status;
1989 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1990 if (status != PJ_SUCCESS)
1991 return status;
1992 break;
1993
1994 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001995 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001996 /* This will close the TCP listener, but existing TCP/TLS
1997 * connections (if any) will still linger
1998 */
1999 status = (*pjsua_var.tpdata[id].data.factory->destroy)
2000 (pjsua_var.tpdata[id].data.factory);
2001 if (status != PJ_SUCCESS)
2002 return status;
2003
Benny Prijono0a5cad82006-09-26 13:21:02 +00002004 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00002005
Benny Prijono1ebd6142006-10-19 15:48:02 +00002006 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00002007 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00002008 }
2009
2010 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00002011 /* If force is not specified, transports will be closed at their
2012 * convenient time. However this will leak PJSUA-API transport
2013 * descriptors as PJSUA-API wouldn't know when exactly the
2014 * transport is closed thus it can't cleanup PJSUA transport
2015 * descriptor.
2016 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00002017 switch (pjsua_var.tpdata[id].type) {
2018 case PJSIP_TRANSPORT_UDP:
2019 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00002020 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00002021 case PJSIP_TRANSPORT_TCP:
2022 return (*pjsua_var.tpdata[id].data.factory->destroy)
2023 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00002024 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00002025 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00002026 }
2027 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002028
Benny Prijono5ff61872007-02-01 03:37:11 +00002029 /* Cleanup pjsua data when force is applied */
2030 if (force) {
2031 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
2032 pjsua_var.tpdata[id].data.ptr = NULL;
2033 }
2034
2035 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002036}
2037
2038
2039/*
2040 * Add additional headers etc in msg_data specified by application
2041 * when sending requests.
2042 */
2043void pjsua_process_msg_data(pjsip_tx_data *tdata,
2044 const pjsua_msg_data *msg_data)
2045{
2046 pj_bool_t allow_body;
2047 const pjsip_hdr *hdr;
2048
Benny Prijono56315612006-07-18 14:39:40 +00002049 /* Always add User-Agent */
2050 if (pjsua_var.ua_cfg.user_agent.slen &&
2051 tdata->msg->type == PJSIP_REQUEST_MSG)
2052 {
2053 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
2054 pjsip_hdr *h;
2055 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
2056 &STR_USER_AGENT,
2057 &pjsua_var.ua_cfg.user_agent);
2058 pjsip_msg_add_hdr(tdata->msg, h);
2059 }
2060
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002061 if (!msg_data)
2062 return;
2063
2064 hdr = msg_data->hdr_list.next;
2065 while (hdr && hdr != &msg_data->hdr_list) {
2066 pjsip_hdr *new_hdr;
2067
Benny Prijonoa1e69682007-05-11 15:14:34 +00002068 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002069 pjsip_msg_add_hdr(tdata->msg, new_hdr);
2070
2071 hdr = hdr->next;
2072 }
2073
2074 allow_body = (tdata->msg->body == NULL);
2075
2076 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
2077 pjsip_media_type ctype;
2078 pjsip_msg_body *body;
2079
2080 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
2081 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
2082 &msg_data->msg_body);
2083 tdata->msg->body = body;
2084 }
2085}
2086
2087
2088/*
2089 * Add route_set to outgoing requests
2090 */
2091void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
2092 const pjsip_route_hdr *route_set )
2093{
2094 const pjsip_route_hdr *r;
2095
2096 r = route_set->next;
2097 while (r != route_set) {
2098 pjsip_route_hdr *new_r;
2099
Benny Prijonoa1e69682007-05-11 15:14:34 +00002100 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002101 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
2102
2103 r = r->next;
2104 }
2105}
2106
2107
2108/*
2109 * Simple version of MIME type parsing (it doesn't support parameters)
2110 */
2111void pjsua_parse_media_type( pj_pool_t *pool,
2112 const pj_str_t *mime,
2113 pjsip_media_type *media_type)
2114{
2115 pj_str_t tmp;
2116 char *pos;
2117
Benny Prijonoac623b32006-07-03 15:19:31 +00002118 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002119
2120 pj_strdup_with_null(pool, &tmp, mime);
2121
2122 pos = pj_strchr(&tmp, '/');
2123 if (pos) {
2124 media_type->type.ptr = tmp.ptr;
2125 media_type->type.slen = (pos-tmp.ptr);
2126 media_type->subtype.ptr = pos+1;
2127 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
2128 } else {
2129 media_type->type = tmp;
2130 }
2131}
2132
2133
2134/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002135 * Internal function to init transport selector from transport id.
2136 */
2137void pjsua_init_tpselector(pjsua_transport_id tp_id,
2138 pjsip_tpselector *sel)
2139{
2140 pjsua_transport_data *tpdata;
2141 unsigned flag;
2142
2143 pj_bzero(sel, sizeof(*sel));
2144 if (tp_id == PJSUA_INVALID_ID)
2145 return;
2146
Benny Prijonoa1e69682007-05-11 15:14:34 +00002147 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002148 tpdata = &pjsua_var.tpdata[tp_id];
2149
2150 flag = pjsip_transport_get_flag_from_type(tpdata->type);
2151
2152 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
2153 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
2154 sel->u.transport = tpdata->data.tp;
2155 } else {
2156 sel->type = PJSIP_TPSELECTOR_LISTENER;
2157 sel->u.listener = tpdata->data.factory;
2158 }
2159}
2160
2161
Benny Prijono6ba8c542007-10-16 01:34:14 +00002162/* Callback upon NAT detection completion */
2163static void nat_detect_cb(void *user_data,
2164 const pj_stun_nat_detect_result *res)
2165{
2166 PJ_UNUSED_ARG(user_data);
2167
2168 pjsua_var.nat_in_progress = PJ_FALSE;
2169 pjsua_var.nat_status = res->status;
2170 pjsua_var.nat_type = res->nat_type;
2171
2172 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
2173 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
2174 }
2175}
2176
2177
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002178/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002179 * Detect NAT type.
2180 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00002181PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002182{
2183 pj_status_t status;
2184
Benny Prijono6ba8c542007-10-16 01:34:14 +00002185 if (pjsua_var.nat_in_progress)
2186 return PJ_SUCCESS;
2187
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002188 /* Make sure STUN server resolution has completed */
Benny Prijonobb995fd2009-08-12 11:03:23 +00002189 status = resolve_stun_server(PJ_TRUE);
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002190 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002191 pjsua_var.nat_status = status;
2192 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002193 return status;
2194 }
2195
2196 /* Make sure we have STUN */
2197 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002198 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
2199 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002200 }
2201
Benny Prijono6ba8c542007-10-16 01:34:14 +00002202 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
2203 &pjsua_var.stun_cfg,
2204 NULL, &nat_detect_cb);
2205
2206 if (status != PJ_SUCCESS) {
2207 pjsua_var.nat_status = status;
2208 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
2209 return status;
2210 }
2211
2212 pjsua_var.nat_in_progress = PJ_TRUE;
2213
2214 return PJ_SUCCESS;
2215}
2216
2217
2218/*
2219 * Get NAT type.
2220 */
2221PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
2222{
2223 *type = pjsua_var.nat_type;
2224 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002225}
2226
2227
2228/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002229 * Verify that valid SIP url is given.
2230 */
2231PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
2232{
2233 pjsip_uri *p;
2234 pj_pool_t *pool;
2235 char *url;
2236 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2237
2238 if (!len) return -1;
2239
2240 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
2241 if (!pool) return -1;
2242
Benny Prijonoa1e69682007-05-11 15:14:34 +00002243 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002244 pj_ansi_strcpy(url, c_url);
2245
2246 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002247 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
2248 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
2249 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002250 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002251 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002252
2253 pj_pool_release(pool);
2254 return p ? 0 : -1;
2255}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002256
2257
Benny Prijono91d06b62008-09-20 12:16:56 +00002258/**
2259 * Normalize route URI (check for ";lr" and append one if it doesn't
2260 * exist and pjsua_config.force_lr is set.
2261 */
2262pj_status_t normalize_route_uri(pj_pool_t *pool, pj_str_t *uri)
2263{
2264 pj_str_t tmp_uri;
2265 pj_pool_t *tmp_pool;
2266 pjsip_uri *uri_obj;
2267 pjsip_sip_uri *sip_uri;
2268
2269 tmp_pool = pjsua_pool_create("tmplr%p", 512, 512);
2270 if (!tmp_pool)
2271 return PJ_ENOMEM;
2272
2273 pj_strdup_with_null(tmp_pool, &tmp_uri, uri);
2274
2275 uri_obj = pjsip_parse_uri(tmp_pool, tmp_uri.ptr, tmp_uri.slen, 0);
2276 if (!uri_obj) {
2277 PJ_LOG(1,(THIS_FILE, "Invalid route URI: %.*s",
2278 (int)uri->slen, uri->ptr));
2279 pj_pool_release(tmp_pool);
2280 return PJSIP_EINVALIDURI;
2281 }
2282
2283 if (!PJSIP_URI_SCHEME_IS_SIP(uri_obj) &&
2284 !PJSIP_URI_SCHEME_IS_SIP(uri_obj))
2285 {
2286 PJ_LOG(1,(THIS_FILE, "Route URI must be SIP URI: %.*s",
2287 (int)uri->slen, uri->ptr));
2288 pj_pool_release(tmp_pool);
2289 return PJSIP_EINVALIDSCHEME;
2290 }
2291
2292 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri_obj);
2293
2294 /* Done if force_lr is disabled or if lr parameter is present */
2295 if (!pjsua_var.ua_cfg.force_lr || sip_uri->lr_param) {
2296 pj_pool_release(tmp_pool);
2297 return PJ_SUCCESS;
2298 }
2299
2300 /* Set lr param */
2301 sip_uri->lr_param = 1;
2302
2303 /* Print the URI */
2304 tmp_uri.ptr = (char*) pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE);
2305 tmp_uri.slen = pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, uri_obj,
2306 tmp_uri.ptr, PJSIP_MAX_URL_SIZE);
2307 if (tmp_uri.slen < 1) {
2308 PJ_LOG(1,(THIS_FILE, "Route URI is too long: %.*s",
2309 (int)uri->slen, uri->ptr));
2310 pj_pool_release(tmp_pool);
2311 return PJSIP_EURITOOLONG;
2312 }
2313
2314 /* Clone the URI */
2315 pj_strdup_with_null(pool, uri, &tmp_uri);
2316
2317 return PJ_SUCCESS;
2318}
2319
Benny Prijonoda9785b2007-04-02 20:43:06 +00002320/*
2321 * This is a utility function to dump the stack states to log, using
2322 * verbosity level 3.
2323 */
2324PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2325{
2326 unsigned old_decor;
2327 unsigned i;
Benny Prijonoda9785b2007-04-02 20:43:06 +00002328
2329 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2330
2331 old_decor = pj_log_get_decor();
2332 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2333
2334 if (detail)
2335 pj_dump_config();
2336
2337 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2338
2339 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2340
2341 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2342 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2343 pjsua_call *call = &pjsua_var.calls[i];
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002344 pjmedia_transport_info tpinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002345 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002346
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002347 /* MSVC complains about tpinfo not being initialized */
Benny Prijono734fc2d2008-03-17 16:05:35 +00002348 //pj_bzero(&tpinfo, sizeof(tpinfo));
Benny Prijono4f093d22007-04-09 08:54:32 +00002349
Benny Prijono734fc2d2008-03-17 16:05:35 +00002350 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002351 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002352
Benny Prijono5186eae2007-12-03 14:38:25 +00002353 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002354 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002355 pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, addr_buf,
Benny Prijono5186eae2007-12-03 14:38:25 +00002356 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002357 }
2358
2359 pjsip_tsx_layer_dump(detail);
2360 pjsip_ua_dump(detail);
2361
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002362// Dumping complete call states may require a 'large' buffer
2363// (about 3KB per call session, including RTCP XR).
2364#if 0
Benny Prijonoda9785b2007-04-02 20:43:06 +00002365 /* Dump all invite sessions: */
2366 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2367
2368 if (pjsua_call_get_count() == 0) {
2369
2370 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2371
2372 } else {
2373 unsigned i;
2374
2375 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2376 if (pjsua_call_is_active(i)) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002377 /* Tricky logging, since call states log string tends to be
2378 * longer than PJ_LOG_MAX_SIZE.
2379 */
2380 char buf[1024 * 3];
2381 unsigned call_dump_len;
2382 unsigned part_len;
2383 unsigned part_idx;
2384 unsigned log_decor;
2385
Benny Prijonoda9785b2007-04-02 20:43:06 +00002386 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002387 call_dump_len = strlen(buf);
2388
2389 log_decor = pj_log_get_decor();
2390 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE |
2391 PJ_LOG_HAS_CR));
2392 PJ_LOG(3,(THIS_FILE, "\n"));
2393 pj_log_set_decor(0);
2394
2395 part_idx = 0;
2396 part_len = PJ_LOG_MAX_SIZE-80;
2397 while (part_idx < call_dump_len) {
2398 char p_orig, *p;
2399
2400 p = &buf[part_idx];
2401 if (part_idx + part_len > call_dump_len)
2402 part_len = call_dump_len - part_idx;
2403 p_orig = p[part_len];
2404 p[part_len] = '\0';
2405 PJ_LOG(3,(THIS_FILE, "%s", p));
2406 p[part_len] = p_orig;
2407 part_idx += part_len;
2408 }
2409 pj_log_set_decor(log_decor);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002410 }
2411 }
2412 }
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002413#endif
Benny Prijonoda9785b2007-04-02 20:43:06 +00002414
2415 /* Dump presence status */
2416 pjsua_pres_dump(detail);
2417
2418 pj_log_set_decor(old_decor);
2419 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2420}
2421