blob: 82dc4d65fdcd40446064c96fb1de424d039c385c [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono268ca612006-02-07 12:34:11 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000021
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono84126ab2006-02-09 09:30:09 +000023#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000024
25
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026/* PJSUA application instance. */
27struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000028
29
Benny Prijono44e88ea2007-10-30 15:42:35 +000030PJ_DEF(struct pjsua_data*) pjsua_get_var(void)
31{
32 return &pjsua_var;
33}
34
35
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036/* Display error */
37PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
38 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000039{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000040 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000041
Benny Prijonoeebe9af2006-06-13 22:57:13 +000042 pj_strerror(status, errmsg, sizeof(errmsg));
43 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000044}
45
46
Benny Prijonoeebe9af2006-06-13 22:57:13 +000047static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000048{
49 unsigned i;
50
Benny Prijonoc97608e2007-03-23 16:34:20 +000051 pj_bzero(&pjsua_var, sizeof(pjsua_var));
52
Benny Prijonoeebe9af2006-06-13 22:57:13 +000053 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
54 pjsua_var.acc[i].index = i;
55
56 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
57 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000058
59 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijono6ba8c542007-10-16 01:34:14 +000060 pjsua_var.nat_status = PJ_EPENDING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000061}
Benny Prijonodc39fe82006-05-26 12:17:46 +000062
63
Benny Prijono1f61a8f2007-08-16 10:11:44 +000064PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
65{
66 pj_bzero(cfg, sizeof(*cfg));
67
68 cfg->msg_logging = PJ_TRUE;
69 cfg->level = 5;
70 cfg->console_level = 4;
71 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
72 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
73}
74
75PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
76 pjsua_logging_config *dst,
77 const pjsua_logging_config *src)
78{
79 pj_memcpy(dst, src, sizeof(*src));
80 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
81}
82
83PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
84{
85 pj_bzero(cfg, sizeof(*cfg));
86
87 cfg->max_calls = 4;
88 cfg->thread_cnt = 1;
Benny Prijono91a6a172007-10-31 08:59:29 +000089 cfg->nat_type_in_sdp = 1;
Benny Prijonod8179652008-01-23 20:39:07 +000090#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
91 cfg->use_srtp = PJSUA_DEFAULT_USE_SRTP;
92 cfg->srtp_secure_signaling = PJSUA_DEFAULT_SRTP_SECURE_SIGNALING;
93#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +000094}
95
Benny Prijono1f61a8f2007-08-16 10:11:44 +000096PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
97 pjsua_config *dst,
98 const pjsua_config *src)
99{
100 unsigned i;
101
102 pj_memcpy(dst, src, sizeof(*src));
103
104 for (i=0; i<src->outbound_proxy_cnt; ++i) {
105 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
106 &src->outbound_proxy[i]);
107 }
108
109 for (i=0; i<src->cred_count; ++i) {
110 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
111 }
112
113 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
114 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
115 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
116 pj_strdup_with_null(pool, &dst->stun_relay_host, &src->stun_relay_host);
117}
118
119PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
120{
121 pj_bzero(msg_data, sizeof(*msg_data));
122 pj_list_init(&msg_data->hdr_list);
123}
124
125PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
126{
127 pj_bzero(cfg, sizeof(*cfg));
128 pjsip_tls_setting_default(&cfg->tls_setting);
129}
130
131PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
132 pjsua_transport_config *dst,
133 const pjsua_transport_config *src)
134{
135 PJ_UNUSED_ARG(pool);
136 pj_memcpy(dst, src, sizeof(*src));
137}
138
139PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
140{
141 pj_bzero(cfg, sizeof(*cfg));
142
143 cfg->reg_timeout = PJSUA_REG_INTERVAL;
144 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000145 cfg->allow_contact_rewrite = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000146 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000147 cfg->ka_interval = 15;
148 cfg->ka_data = pj_str("\r\n");
Benny Prijonod8179652008-01-23 20:39:07 +0000149#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
150 cfg->use_srtp = pjsua_var.ua_cfg.use_srtp;
151 cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling;
152#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000153}
154
155PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
156{
157 pj_bzero(cfg, sizeof(*cfg));
158}
159
160PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
161{
162 pj_bzero(cfg, sizeof(*cfg));
163
164 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono50f19b32008-03-11 13:15:43 +0000165 cfg->snd_clock_rate = 0;
Benny Prijono7d60d052008-03-29 12:24:20 +0000166 cfg->channel_count = 1;
Benny Prijono37c710b2008-01-10 12:09:26 +0000167 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
168 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000169 cfg->has_ioqueue = PJ_TRUE;
170 cfg->thread_cnt = 1;
171 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
172 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
173 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
174 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
175}
176
Benny Prijonodc39fe82006-05-26 12:17:46 +0000177
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000178/*****************************************************************************
179 * This is a very simple PJSIP module, whose sole purpose is to display
180 * incoming and outgoing messages to log. This module will have priority
181 * higher than transport layer, which means:
182 *
183 * - incoming messages will come to this module first before reaching
184 * transaction layer.
185 *
186 * - outgoing messages will come to this module last, after the message
187 * has been 'printed' to contiguous buffer by transport layer and
188 * appropriate transport instance has been decided for this message.
189 *
190 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000191
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000192/* Notification on incoming messages */
193static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
194{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000195 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
196 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000197 "--end msg--",
198 rdata->msg_info.len,
199 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000200 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000201 rdata->pkt_info.src_name,
202 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000203 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000204 rdata->msg_info.msg_buf));
205
206 /* Always return false, otherwise messages will not get processed! */
207 return PJ_FALSE;
208}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000209
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000210/* Notification on outgoing messages */
211static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
212{
213
214 /* Important note:
215 * tp_info field is only valid after outgoing messages has passed
216 * transport layer. So don't try to access tp_info when the module
217 * has lower priority than transport layer.
218 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000219
Benny Prijonob4a17c92006-07-10 14:40:21 +0000220 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
221 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000222 "--end msg--",
223 (tdata->buf.cur - tdata->buf.start),
224 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000225 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000226 tdata->tp_info.dst_name,
227 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000228 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000229 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000230
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000231 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000232 return PJ_SUCCESS;
233}
234
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000235/* The module instance. */
236static pjsip_module pjsua_msg_logger =
237{
238 NULL, NULL, /* prev, next. */
239 { "mod-pjsua-log", 13 }, /* Name. */
240 -1, /* Id */
241 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
242 NULL, /* load() */
243 NULL, /* start() */
244 NULL, /* stop() */
245 NULL, /* unload() */
246 &logging_on_rx_msg, /* on_rx_request() */
247 &logging_on_rx_msg, /* on_rx_response() */
248 &logging_on_tx_msg, /* on_tx_request. */
249 &logging_on_tx_msg, /* on_tx_response() */
250 NULL, /* on_tsx_state() */
251
252};
253
254
255/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000256 * Another simple module to handle incoming OPTIONS request
257 */
258
259/* Notification on incoming request */
260static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
261{
262 pjsip_tx_data *tdata;
263 pjsip_response_addr res_addr;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000264 pjmedia_transport_info tpinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000265 pjmedia_sdp_session *sdp;
266 const pjsip_hdr *cap_hdr;
267 pj_status_t status;
268
269 /* Only want to handle OPTIONS requests */
270 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000271 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000272 {
273 return PJ_FALSE;
274 }
275
276 /* Create basic response. */
277 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
278 &tdata);
279 if (status != PJ_SUCCESS) {
280 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
281 return PJ_TRUE;
282 }
283
284 /* Add Allow header */
285 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
286 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000287 pjsip_msg_add_hdr(tdata->msg,
288 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000289 }
290
291 /* Add Accept header */
292 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
293 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000294 pjsip_msg_add_hdr(tdata->msg,
295 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000296 }
297
298 /* Add Supported header */
299 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
300 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000301 pjsip_msg_add_hdr(tdata->msg,
302 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000303 }
304
305 /* Add Allow-Events header from the evsub module */
306 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
307 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000308 pjsip_msg_add_hdr(tdata->msg,
309 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000310 }
311
312 /* Add User-Agent header */
313 if (pjsua_var.ua_cfg.user_agent.slen) {
314 const pj_str_t USER_AGENT = { "User-Agent", 10};
315 pjsip_hdr *h;
316
317 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
318 &USER_AGENT,
319 &pjsua_var.ua_cfg.user_agent);
320 pjsip_msg_add_hdr(tdata->msg, h);
321 }
322
Benny Prijono617c5bc2007-04-02 19:51:21 +0000323 /* Get media socket info */
Benny Prijono734fc2d2008-03-17 16:05:35 +0000324 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000325 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000326
Benny Prijono56315612006-07-18 14:39:40 +0000327 /* Add SDP body, using call0's RTP address */
328 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000329 &tpinfo.sock_info, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000330 if (status == PJ_SUCCESS) {
331 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
332 }
333
334 /* Send response statelessly */
335 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
336 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
337 if (status != PJ_SUCCESS)
338 pjsip_tx_data_dec_ref(tdata);
339
340 return PJ_TRUE;
341}
342
343
344/* The module instance. */
345static pjsip_module pjsua_options_handler =
346{
347 NULL, NULL, /* prev, next. */
348 { "mod-pjsua-options", 17 }, /* Name. */
349 -1, /* Id */
350 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
351 NULL, /* load() */
352 NULL, /* start() */
353 NULL, /* stop() */
354 NULL, /* unload() */
355 &options_on_rx_request, /* on_rx_request() */
356 NULL, /* on_rx_response() */
357 NULL, /* on_tx_request. */
358 NULL, /* on_tx_response() */
359 NULL, /* on_tsx_state() */
360
361};
362
363
364/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000365 * These two functions are the main callbacks registered to PJSIP stack
366 * to receive SIP request and response messages that are outside any
367 * dialogs and any transactions.
368 */
Benny Prijono268ca612006-02-07 12:34:11 +0000369
370/*
371 * Handler for receiving incoming requests.
372 *
373 * This handler serves multiple purposes:
374 * - it receives requests outside dialogs.
375 * - it receives requests inside dialogs, when the requests are
376 * unhandled by other dialog usages. Example of these
377 * requests are: MESSAGE.
378 */
379static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
380{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000381 pj_bool_t processed = PJ_FALSE;
382
383 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000384
Benny Prijono84126ab2006-02-09 09:30:09 +0000385 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000386
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000387 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000388 }
389
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390 PJSUA_UNLOCK();
391
392 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000393}
394
395
396/*
397 * Handler for receiving incoming responses.
398 *
399 * This handler serves multiple purposes:
400 * - it receives strayed responses (i.e. outside any dialog and
401 * outside any transactions).
402 * - it receives responses coming to a transaction, when pjsua
403 * module is set as transaction user for the transaction.
404 * - it receives responses inside a dialog, when these responses
405 * are unhandled by other dialog usages.
406 */
407static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
408{
409 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000410 return PJ_FALSE;
411}
412
413
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000414/*****************************************************************************
415 * Logging.
416 */
417
418/* Log callback */
419static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000420{
Benny Prijono572d4852006-11-23 21:50:02 +0000421 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000422
423 if (pjsua_var.log_file) {
424 pj_ssize_t size = len;
425 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000426 /* This will slow things down considerably! Don't do it!
427 pj_file_flush(pjsua_var.log_file);
428 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000429 }
430
Benny Prijono572d4852006-11-23 21:50:02 +0000431 if (level <= (int)pjsua_var.log_cfg.console_level) {
432 if (pjsua_var.log_cfg.cb)
433 (*pjsua_var.log_cfg.cb)(level, buffer, len);
434 else
435 pj_log_write(level, buffer, len);
436 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000437}
438
439
440/*
441 * Application can call this function at any time (after pjsua_create(), of
442 * course) to change logging settings.
443 */
444PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
445{
446 pj_status_t status;
447
448 /* Save config. */
449 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
450
451 /* Redirect log function to ours */
452 pj_log_set_log_func( &log_writer );
453
Benny Prijono9cb09a22007-08-30 09:35:10 +0000454 /* Set decor */
455 pj_log_set_decor(pjsua_var.log_cfg.decor);
456
Benny Prijono4190cf92008-01-18 13:25:05 +0000457 /* Set log level */
458 pj_log_set_level(pjsua_var.log_cfg.level);
459
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000460 /* Close existing file, if any */
461 if (pjsua_var.log_file) {
462 pj_file_close(pjsua_var.log_file);
463 pjsua_var.log_file = NULL;
464 }
465
466 /* If output log file is desired, create the file: */
467 if (pjsua_var.log_cfg.log_filename.slen) {
468
469 status = pj_file_open(pjsua_var.pool,
470 pjsua_var.log_cfg.log_filename.ptr,
471 PJ_O_WRONLY,
472 &pjsua_var.log_file);
473
474 if (status != PJ_SUCCESS) {
475 pjsua_perror(THIS_FILE, "Error creating log file", status);
476 return status;
477 }
478 }
479
480 /* Unregister msg logging if it's previously registered */
481 if (pjsua_msg_logger.id >= 0) {
482 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
483 pjsua_msg_logger.id = -1;
484 }
485
486 /* Enable SIP message logging */
487 if (pjsua_var.log_cfg.msg_logging)
488 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
489
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000490 return PJ_SUCCESS;
491}
492
493
494/*****************************************************************************
495 * PJSUA Base API.
496 */
497
498/* Worker thread function. */
499static int worker_thread(void *arg)
500{
501 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000502
Benny Prijono268ca612006-02-07 12:34:11 +0000503 PJ_UNUSED_ARG(arg);
504
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000505 while (!pjsua_var.thread_quit_flag) {
506 int count;
507
508 count = pjsua_handle_events(TIMEOUT);
509 if (count < 0)
510 pj_thread_sleep(TIMEOUT);
511 }
Benny Prijono268ca612006-02-07 12:34:11 +0000512
513 return 0;
514}
515
Benny Prijonodc39fe82006-05-26 12:17:46 +0000516
Benny Prijono8389c312008-02-21 21:36:34 +0000517/* Init random seed */
518static void init_random_seed(void)
519{
520 pj_sockaddr addr;
521 const pj_str_t *hostname;
522 pj_uint32_t pid;
523 pj_time_val t;
524 unsigned seed=0;
525
526 /* Add hostname */
527 hostname = pj_gethostname();
528 seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen);
529
530 /* Add primary IP address */
531 if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS)
532 seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4);
533
534 /* Get timeofday */
535 pj_gettimeofday(&t);
536 seed = pj_hash_calc(seed, &t, sizeof(t));
537
538 /* Add PID */
539 pid = pj_getpid();
540 seed = pj_hash_calc(seed, &pid, sizeof(pid));
541
542 /* Init random seed */
543 pj_srand(seed);
544}
545
Benny Prijono268ca612006-02-07 12:34:11 +0000546/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000547 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000548 */
549PJ_DEF(pj_status_t) pjsua_create(void)
550{
551 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000552
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000553 /* Init pjsua data */
554 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000555
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000556 /* Set default logging settings */
557 pjsua_logging_config_default(&pjsua_var.log_cfg);
558
559 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000560 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000561 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
562
Benny Prijono8389c312008-02-21 21:36:34 +0000563 /* Init random seed */
564 init_random_seed();
Benny Prijono268ca612006-02-07 12:34:11 +0000565
Benny Prijonofccab712006-02-22 22:23:22 +0000566 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000567 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000568 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000569
Benny Prijonoec921342007-03-24 13:00:30 +0000570 /* Init PJNATH */
571 status = pjnath_init();
572 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000573
Benny Prijono094d3ad2006-11-21 08:41:00 +0000574 /* Set default sound device ID */
575 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
576
Benny Prijono268ca612006-02-07 12:34:11 +0000577 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000578 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000579
580 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000581 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
582
583 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000584
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000585 /* Create mutex */
586 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
587 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000588 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000589 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000590 return status;
591 }
592
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000593 /* Must create SIP endpoint to initialize SIP parser. The parser
594 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000595 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000596 status = pjsip_endpt_create(&pjsua_var.cp.factory,
597 pj_gethostname()->ptr,
598 &pjsua_var.endpt);
599 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000600
601
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000602 return PJ_SUCCESS;
603}
604
605
606/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000607 * Initialize pjsua with the specified settings. All the settings are
608 * optional, and the default values will be used when the config is not
609 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000610 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000611PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
612 const pjsua_logging_config *log_cfg,
613 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000614{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000615 pjsua_config default_cfg;
616 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000617 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000618 pj_status_t status;
619
620
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000621 /* Create default configurations when the config is not supplied */
622
623 if (ua_cfg == NULL) {
624 pjsua_config_default(&default_cfg);
625 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000626 }
627
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000628 if (media_cfg == NULL) {
629 pjsua_media_config_default(&default_media_cfg);
630 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000631 }
632
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000633 /* Initialize logging first so that info/errors can be captured */
634 if (log_cfg) {
635 status = pjsua_reconfigure_logging(log_cfg);
636 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000637 }
638
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000639 /* If nameserver is configured, create DNS resolver instance and
640 * set it to be used by SIP resolver.
641 */
642 if (ua_cfg->nameserver_count) {
643#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000644 unsigned i;
645
646 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000647 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
648 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000649 if (status != PJ_SUCCESS) {
650 pjsua_perror(THIS_FILE, "Error creating resolver", status);
651 return status;
652 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000653
654 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000655 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
656 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000657 ua_cfg->nameserver, NULL);
658 if (status != PJ_SUCCESS) {
659 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
660 return status;
661 }
662
663 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000664 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000665 if (status != PJ_SUCCESS) {
666 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
667 return status;
668 }
669
670 /* Print nameservers */
671 for (i=0; i<ua_cfg->nameserver_count; ++i) {
672 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
673 (int)ua_cfg->nameserver[i].slen,
674 ua_cfg->nameserver[i].ptr));
675 }
676#else
677 PJ_LOG(2,(THIS_FILE,
678 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
679#endif
680 }
681
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000682 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000683
684 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000685 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
686 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000687
Benny Prijonodc39fe82006-05-26 12:17:46 +0000688
689 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000690 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
691 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000692
Benny Prijonodc39fe82006-05-26 12:17:46 +0000693
Benny Prijono053f5222006-11-11 16:16:04 +0000694 /* Initialize Replaces support. */
695 status = pjsip_replaces_init_module( pjsua_var.endpt );
696 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
697
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000698 /* Initialize 100rel support */
699 status = pjsip_100rel_init_module(pjsua_var.endpt);
700 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000701
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000702 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000703 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000705 {
706 NULL, NULL, /* prev, next. */
707 { "mod-pjsua", 9 }, /* Name. */
708 -1, /* Id */
709 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
710 NULL, /* load() */
711 NULL, /* start() */
712 NULL, /* stop() */
713 NULL, /* unload() */
714 &mod_pjsua_on_rx_request, /* on_rx_request() */
715 &mod_pjsua_on_rx_response, /* on_rx_response() */
716 NULL, /* on_tx_request. */
717 NULL, /* on_tx_response() */
718 NULL, /* on_tsx_state() */
719 };
720
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000722
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000723 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
724 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000725 }
726
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000727
Benny Prijonodc39fe82006-05-26 12:17:46 +0000728
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000729 /* Initialize PJSUA call subsystem: */
730 status = pjsua_call_subsys_init(ua_cfg);
731 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000732 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000733
734
Benny Prijonoc97608e2007-03-23 16:34:20 +0000735 /* Start resolving STUN server */
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000736
Benny Prijonoc97608e2007-03-23 16:34:20 +0000737 status = pjsua_resolve_stun_server(PJ_FALSE);
738 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
739 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
740 return status;
741 }
742
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000743 /* Initialize PJSUA media subsystem */
744 status = pjsua_media_subsys_init(media_cfg);
745 if (status != PJ_SUCCESS)
746 goto on_error;
747
Benny Prijonodc39fe82006-05-26 12:17:46 +0000748
749 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000750 status = pjsip_evsub_init_module(pjsua_var.endpt);
751 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000752
Benny Prijonodc39fe82006-05-26 12:17:46 +0000753
754 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000755 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
756 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000757
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000758 /* Init PUBLISH module */
759 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000760
761 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000762 status = pjsip_xfer_init_module( pjsua_var.endpt );
763 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000764
765 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000766 status = pjsua_pres_init();
767 if (status != PJ_SUCCESS)
768 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000769
770 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000771 status = pjsua_im_init();
772 if (status != PJ_SUCCESS)
773 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000774
Benny Prijonoc8141a82006-08-20 09:12:19 +0000775 /* Register OPTIONS handler */
776 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
777
778 /* Add OPTIONS in Allow header */
779 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
780 NULL, 1, &STR_OPTIONS);
781
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000782 /* Start worker thread if needed. */
783 if (pjsua_var.ua_cfg.thread_cnt) {
784 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000785
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000786 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
787 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000788
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000789 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
790 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
791 NULL, 0, 0, &pjsua_var.thread[i]);
792 if (status != PJ_SUCCESS)
793 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000794 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000795 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
796 pjsua_var.ua_cfg.thread_cnt));
797 } else {
798 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000799 }
800
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000801 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000802
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000803 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000804 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000805
Benny Prijono268ca612006-02-07 12:34:11 +0000806 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000807
808on_error:
809 pjsua_destroy();
810 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000811}
812
813
Benny Prijono834aee32006-02-19 01:38:06 +0000814/* Sleep with polling */
815static void busy_sleep(unsigned msec)
816{
Benny Prijonob2c96822007-05-03 13:31:21 +0000817#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Benny Prijono897f9f82007-05-03 19:56:21 +0000818 /* Ideally we shouldn't call pj_thread_sleep() and rather
819 * CActiveScheduler::WaitForAnyRequest() here, but that will
820 * drag in Symbian header and it doesn't look pretty.
821 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000822 pj_thread_sleep(msec);
823#else
Benny Prijono834aee32006-02-19 01:38:06 +0000824 pj_time_val timeout, now;
825
826 pj_gettimeofday(&timeout);
827 timeout.msec += msec;
828 pj_time_val_normalize(&timeout);
829
830 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000831 while (pjsua_handle_events(10) > 0)
832 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000833 pj_gettimeofday(&now);
834 } while (PJ_TIME_VAL_LT(now, timeout));
Benny Prijonob2c96822007-05-03 13:31:21 +0000835#endif
Benny Prijono834aee32006-02-19 01:38:06 +0000836}
837
Benny Prijonoebbf6892007-03-24 17:37:25 +0000838
Benny Prijonoebbf6892007-03-24 17:37:25 +0000839/*
840 * Callback function to receive notification from the resolver
841 * when the resolution process completes.
842 */
843static void stun_dns_srv_resolver_cb(void *user_data,
844 pj_status_t status,
845 const pj_dns_srv_record *rec)
846{
847 unsigned i;
848
849 PJ_UNUSED_ARG(user_data);
850
851 pjsua_var.stun_status = status;
852
853 if (status != PJ_SUCCESS) {
854 /* DNS SRV resolution failed. If stun_host is specified, resolve
855 * it with gethostbyname()
856 */
857 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000858 pj_str_t str_host, str_port;
859 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000860 pj_hostent he;
861
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000862 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
863 if (str_port.ptr != NULL) {
864 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
865 str_host.slen = (str_port.ptr - str_host.ptr);
866 str_port.ptr++;
867 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
868 str_host.slen - 1;
869 port = (int)pj_strtoul(&str_port);
870 if (port < 1 || port > 65535) {
871 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
872 pjsua_var.stun_status = PJ_EINVAL;
873 return;
874 }
875 } else {
876 str_host = pjsua_var.ua_cfg.stun_host;
877 port = 3478;
878 }
879
880 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000881
882 if (pjsua_var.stun_status == PJ_SUCCESS) {
883 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
884 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000885 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000886
887 PJ_LOG(3,(THIS_FILE,
888 "STUN server %.*s resolved, address is %s:%d",
889 (int)pjsua_var.ua_cfg.stun_host.slen,
890 pjsua_var.ua_cfg.stun_host.ptr,
891 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
892 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
893 }
894 } else {
895 char errmsg[PJ_ERR_MSG_SIZE];
896
897 pj_strerror(status, errmsg, sizeof(errmsg));
898 PJ_LOG(1,(THIS_FILE,
899 "DNS SRV resolution failed for STUN server %.*s: %s",
900 (int)pjsua_var.ua_cfg.stun_domain.slen,
901 pjsua_var.ua_cfg.stun_domain.ptr,
902 errmsg));
903 }
904 return;
905 }
906
Benny Prijonof5afa922007-06-11 16:51:18 +0000907 pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0);
908 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL,
909 rec->entry[0].port);
910 pjsua_var.stun_srv.ipv4.sin_addr.s_addr =
911 rec->entry[0].server.addr[0].s_addr;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000912
913 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
914 (int)pjsua_var.ua_cfg.stun_domain.slen,
915 pjsua_var.ua_cfg.stun_domain.ptr,
916 rec->count));
917
918 for (i=0; i<rec->count; ++i) {
919 PJ_LOG(3,(THIS_FILE,
920 " %d: prio=%d, weight=%d %s:%d",
921 i, rec->entry[i].priority, rec->entry[i].weight,
Benny Prijonof5afa922007-06-11 16:51:18 +0000922 pj_inet_ntoa(rec->entry[i].server.addr[0]),
923 (int)rec->entry[i].port));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000924 }
925
926}
927
Benny Prijono268ca612006-02-07 12:34:11 +0000928/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000929 * Resolve STUN server.
930 */
931pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
932{
933 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
934 /* Initialize STUN configuration */
935 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
936 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
937 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
938
939 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000940
941 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000942
Benny Prijonoebbf6892007-03-24 17:37:25 +0000943 /* If stun_domain is specified, resolve STUN servers with DNS
944 * SRV resolution.
945 */
946 if (pjsua_var.ua_cfg.stun_domain.slen) {
947 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000948 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000949
950 /* Fail if resolver is not configured */
951 if (pjsua_var.resolver == NULL) {
952 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
953 "stun_domain is specified"));
954 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
955 return PJLIB_UTIL_EDNSNONS;
956 }
957 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000958 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000959 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
960 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonof5afa922007-06-11 16:51:18 +0000961 0, NULL, &stun_dns_srv_resolver_cb, NULL);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000962 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000963 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
964 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000965 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000966 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000967 } else {
968 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000969 }
970 }
971 /* Otherwise if stun_host is specified, resolve STUN server with
972 * gethostbyname().
973 */
974 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000975 pj_str_t str_host, str_port;
976 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000977 pj_hostent he;
978
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000979 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
980 if (str_port.ptr != NULL) {
981 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
982 str_host.slen = (str_port.ptr - str_host.ptr);
983 str_port.ptr++;
984 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
985 str_host.slen - 1;
986 port = (int)pj_strtoul(&str_port);
987 if (port < 1 || port > 65535) {
988 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
989 pjsua_var.stun_status = PJ_EINVAL;
990 return pjsua_var.stun_status;
991 }
992 } else {
993 str_host = pjsua_var.ua_cfg.stun_host;
994 port = 3478;
995 }
996
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000997 pjsua_var.stun_status =
998 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, &str_host,
999 (pj_uint16_t)port);
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001000
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001001 if (pjsua_var.stun_status != PJ_SUCCESS) {
1002 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001003
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001004 if (pjsua_var.stun_status == PJ_SUCCESS) {
1005 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
1006 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
1007 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
1008 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001009 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001010
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001011 PJ_LOG(3,(THIS_FILE,
1012 "STUN server %.*s resolved, address is %s:%d",
1013 (int)pjsua_var.ua_cfg.stun_host.slen,
1014 pjsua_var.ua_cfg.stun_host.ptr,
1015 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
1016 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
1017
Benny Prijonoc97608e2007-03-23 16:34:20 +00001018 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001019 /* Otherwise disable STUN. */
1020 else {
1021 pjsua_var.stun_status = PJ_SUCCESS;
1022 }
1023
1024
Benny Prijonoc97608e2007-03-23 16:34:20 +00001025 return pjsua_var.stun_status;
1026
1027 } else if (pjsua_var.stun_status == PJ_EPENDING) {
1028 /* STUN server resolution has been started, wait for the
1029 * result.
1030 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001031 if (wait) {
1032 while (pjsua_var.stun_status == PJ_EPENDING)
1033 pjsua_handle_events(10);
1034 }
1035
1036 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001037
1038 } else {
1039 /* STUN server has been resolved, return the status */
1040 return pjsua_var.stun_status;
1041 }
1042}
1043
1044/*
Benny Prijono268ca612006-02-07 12:34:11 +00001045 * Destroy pjsua.
1046 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001047PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001048{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001049 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001050
1051 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001052 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001053
1054 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001055 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1056 if (pjsua_var.thread[i]) {
1057 pj_thread_join(pjsua_var.thread[i]);
1058 pj_thread_destroy(pjsua_var.thread[i]);
1059 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001060 }
Benny Prijono268ca612006-02-07 12:34:11 +00001061 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001062
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001063 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001064 /* Terminate all calls. */
1065 pjsua_call_hangup_all();
1066
1067 /* Terminate all presence subscriptions. */
1068 pjsua_pres_shutdown();
1069
1070 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001071 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1072 if (!pjsua_var.acc[i].valid)
1073 continue;
1074
1075 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001076 pjsua_acc_set_registration(i, PJ_FALSE);
1077 }
1078 }
1079
1080 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001081 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1082 busy_sleep(1000);
1083 }
Benny Prijono834aee32006-02-19 01:38:06 +00001084
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001085 /* Destroy media */
1086 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001087
Benny Prijono268ca612006-02-07 12:34:11 +00001088 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001089 if (pjsua_var.endpt) {
1090 pjsip_endpt_destroy(pjsua_var.endpt);
1091 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001092 }
Benny Prijono268ca612006-02-07 12:34:11 +00001093
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001094 /* Destroy mutex */
1095 if (pjsua_var.mutex) {
1096 pj_mutex_destroy(pjsua_var.mutex);
1097 pjsua_var.mutex = NULL;
1098 }
1099
1100 /* Destroy pool and pool factory. */
1101 if (pjsua_var.pool) {
1102 pj_pool_release(pjsua_var.pool);
1103 pjsua_var.pool = NULL;
1104 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001105
1106 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1107
1108 /* End logging */
1109 if (pjsua_var.log_file) {
1110 pj_file_close(pjsua_var.log_file);
1111 pjsua_var.log_file = NULL;
1112 }
1113
1114 /* Shutdown PJLIB */
1115 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001116 }
Benny Prijono268ca612006-02-07 12:34:11 +00001117
Benny Prijonof762ee72006-12-01 11:14:37 +00001118 /* Clear pjsua_var */
1119 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1120
Benny Prijono268ca612006-02-07 12:34:11 +00001121 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001122 return PJ_SUCCESS;
1123}
1124
1125
1126/**
1127 * Application is recommended to call this function after all initialization
1128 * is done, so that the library can do additional checking set up
1129 * additional
1130 *
1131 * @return PJ_SUCCESS on success, or the appropriate error code.
1132 */
1133PJ_DEF(pj_status_t) pjsua_start(void)
1134{
1135 pj_status_t status;
1136
1137 status = pjsua_call_subsys_start();
1138 if (status != PJ_SUCCESS)
1139 return status;
1140
1141 status = pjsua_media_subsys_start();
1142 if (status != PJ_SUCCESS)
1143 return status;
1144
1145 status = pjsua_pres_start();
1146 if (status != PJ_SUCCESS)
1147 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001148
1149 return PJ_SUCCESS;
1150}
1151
Benny Prijono9fc735d2006-05-28 14:58:12 +00001152
1153/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001154 * Poll pjsua for events, and if necessary block the caller thread for
1155 * the specified maximum interval (in miliseconds).
1156 */
1157PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1158{
Benny Prijono897f9f82007-05-03 19:56:21 +00001159#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
1160 /* Ideally we shouldn't call pj_thread_sleep() and rather
1161 * CActiveScheduler::WaitForAnyRequest() here, but that will
1162 * drag in Symbian header and it doesn't look pretty.
1163 */
1164 pj_thread_sleep(msec_timeout);
1165 return msec_timeout;
1166#else
1167
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001168 unsigned count = 0;
1169 pj_time_val tv;
1170 pj_status_t status;
1171
1172 tv.sec = 0;
1173 tv.msec = msec_timeout;
1174 pj_time_val_normalize(&tv);
1175
1176 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1177
1178 if (status != PJ_SUCCESS)
1179 return -status;
1180
1181 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001182#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001183}
1184
1185
1186/*
1187 * Create memory pool.
1188 */
1189PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1190 pj_size_t increment)
1191{
1192 /* Pool factory is thread safe, no need to lock */
1193 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1194 NULL);
1195}
1196
1197
1198/*
1199 * Internal function to get SIP endpoint instance of pjsua, which is
1200 * needed for example to register module, create transports, etc.
1201 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001202 */
1203PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1204{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001206}
1207
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001208/*
1209 * Internal function to get media endpoint instance.
1210 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001211 */
1212PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1213{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001214 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001215}
1216
Benny Prijono97b87172006-08-24 14:25:14 +00001217/*
1218 * Internal function to get PJSUA pool factory.
1219 */
1220PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1221{
1222 return &pjsua_var.cp.factory;
1223}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001224
1225/*****************************************************************************
1226 * PJSUA SIP Transport API.
1227 */
1228
1229/*
Benny Prijono23674a32007-12-01 08:59:25 +00001230 * Tools to get address string.
1231 */
1232static const char *addr_string(const pj_sockaddr_t *addr)
1233{
1234 static char str[128];
1235 str[0] = '\0';
1236 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1237 pj_sockaddr_get_addr(addr),
1238 str, sizeof(str));
1239 return str;
1240}
1241
1242/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001243 * Create and initialize SIP socket (and possibly resolve public
1244 * address via STUN, depending on config).
1245 */
Benny Prijono23674a32007-12-01 08:59:25 +00001246static pj_status_t create_sip_udp_sock(int af,
1247 const pj_str_t *bind_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001248 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001249 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001250 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001251{
Benny Prijono23674a32007-12-01 08:59:25 +00001252 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijonoc97608e2007-03-23 16:34:20 +00001253 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001254 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001255 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001256 pj_status_t status;
1257
Benny Prijonoc97608e2007-03-23 16:34:20 +00001258 /* Make sure STUN server resolution has completed */
1259 status = pjsua_resolve_stun_server(PJ_TRUE);
1260 if (status != PJ_SUCCESS) {
1261 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1262 return status;
1263 }
1264
Benny Prijono23674a32007-12-01 08:59:25 +00001265 /* Initialize bound address */
1266 if (bind_param->slen) {
1267 status = pj_sockaddr_init(af, &bind_addr, bind_param,
1268 (pj_uint16_t)port);
1269 if (status != PJ_SUCCESS) {
1270 pjsua_perror(THIS_FILE,
1271 "Unable to resolve transport bound address",
1272 status);
1273 return status;
1274 }
1275 } else {
1276 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1277 }
1278
1279 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001280 if (status != PJ_SUCCESS) {
1281 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001282 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001283 }
1284
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001285 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001286 if (status != PJ_SUCCESS) {
1287 pjsua_perror(THIS_FILE, "bind() error", status);
1288 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001289 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001290 }
1291
Benny Prijonoe347cb02007-02-14 14:36:13 +00001292 /* If port is zero, get the bound port */
1293 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001294 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001295 int namelen = sizeof(bound_addr);
1296 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1297 if (status != PJ_SUCCESS) {
1298 pjsua_perror(THIS_FILE, "getsockname() error", status);
1299 pj_sock_close(sock);
1300 return status;
1301 }
1302
Benny Prijono23674a32007-12-01 08:59:25 +00001303 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001304 }
1305
Benny Prijonoc97608e2007-03-23 16:34:20 +00001306 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001307 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1308 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001309 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001310 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001311 }
1312
1313 /* Get the published address, either by STUN or by resolving
1314 * the name of local host.
1315 */
Benny Prijono23674a32007-12-01 08:59:25 +00001316 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001317 /*
1318 * Public address is already specified, no need to resolve the
1319 * address, only set the port.
1320 */
Benny Prijono23674a32007-12-01 08:59:25 +00001321 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1322 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001323
1324 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001325 /*
1326 * STUN is specified, resolve the address with STUN.
1327 */
Benny Prijono23674a32007-12-01 08:59:25 +00001328 if (af != pj_AF_INET()) {
1329 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1330 pj_sock_close(sock);
1331 return PJ_EAFNOTSUP;
1332 }
1333
Benny Prijono14c2b862007-02-21 00:40:05 +00001334 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001335 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1336 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001337 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001338 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001339 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001340 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001341 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001342 }
1343
1344 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001345 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001346
Benny Prijono42d08d22007-12-20 11:23:07 +00001347 if (pj_sockaddr_has_addr(&bind_addr)) {
1348 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1349 } else {
1350 status = pj_gethostip(af, p_pub_addr);
1351 if (status != PJ_SUCCESS) {
1352 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1353 pj_sock_close(sock);
1354 return status;
1355 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001356 }
1357
Benny Prijono23674a32007-12-01 08:59:25 +00001358 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1359 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001360 }
1361
1362 *p_sock = sock;
1363
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001364 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001365 addr_string(p_pub_addr),
1366 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001367
Benny Prijonod8410532006-06-15 11:04:33 +00001368 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001369}
1370
1371
1372/*
1373 * Create SIP transport.
1374 */
1375PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1376 const pjsua_transport_config *cfg,
1377 pjsua_transport_id *p_id)
1378{
1379 pjsip_transport *tp;
1380 unsigned id;
1381 pj_status_t status;
1382
1383 PJSUA_LOCK();
1384
1385 /* Find empty transport slot */
1386 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001387 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001388 break;
1389 }
1390
1391 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1392 status = PJ_ETOOMANY;
1393 pjsua_perror(THIS_FILE, "Error creating transport", status);
1394 goto on_return;
1395 }
1396
1397 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001398 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001399 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001400 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001401 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001402 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001403 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001404 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001405 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001406 pjsip_host_port addr_name;
1407
1408 /* Supply default config if it's not specified */
1409 if (cfg == NULL) {
1410 pjsua_transport_config_default(&config);
1411 cfg = &config;
1412 }
1413
Benny Prijono0a5cad82006-09-26 13:21:02 +00001414 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001415 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1416 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001417 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001418 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1419 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001420 if (status != PJ_SUCCESS) {
1421 pjsua_perror(THIS_FILE,
1422 "Unable to resolve transport public address",
1423 status);
1424 goto on_return;
1425 }
1426 }
1427
1428 /* Create the socket and possibly resolve the address with STUN
1429 * (only when public address is not specified).
1430 */
Benny Prijono23674a32007-12-01 08:59:25 +00001431 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
1432 &cfg->bound_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001433 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001434 if (status != PJ_SUCCESS)
1435 goto on_return;
1436
Benny Prijono23674a32007-12-01 08:59:25 +00001437 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1438 addr_name.host = pj_str(hostbuf);
1439 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001440
1441 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001442 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1443 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001444 if (status != PJ_SUCCESS) {
1445 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1446 status);
1447 pj_sock_close(sock);
1448 goto on_return;
1449 }
1450
Benny Prijonoe93e2872006-06-28 16:46:49 +00001451
1452 /* Save the transport */
1453 pjsua_var.tpdata[id].type = type;
1454 pjsua_var.tpdata[id].local_name = tp->local_name;
1455 pjsua_var.tpdata[id].data.tp = tp;
1456
Benny Prijono3569c0d2007-04-06 10:29:20 +00001457#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1458
Benny Prijonob2477142007-12-05 04:09:59 +00001459 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001460 /*
1461 * Create TCP transport.
1462 */
1463 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001464 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001465 pjsip_tpfactory *tcp;
1466 pj_sockaddr_in local_addr;
1467
1468 /* Supply default config if it's not specified */
1469 if (cfg == NULL) {
1470 pjsua_transport_config_default(&config);
1471 cfg = &config;
1472 }
1473
1474 /* Init local address */
1475 pj_sockaddr_in_init(&local_addr, 0, 0);
1476
1477 if (cfg->port)
1478 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1479
Benny Prijono0a5cad82006-09-26 13:21:02 +00001480 if (cfg->bound_addr.slen) {
1481 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1482 if (status != PJ_SUCCESS) {
1483 pjsua_perror(THIS_FILE,
1484 "Unable to resolve transport bound address",
1485 status);
1486 goto on_return;
1487 }
1488 }
1489
1490 /* Init published name */
1491 pj_bzero(&a_name, sizeof(pjsip_host_port));
1492 if (cfg->public_addr.slen)
1493 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001494
1495 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001496 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1497 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001498
1499 if (status != PJ_SUCCESS) {
1500 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1501 status);
1502 goto on_return;
1503 }
1504
1505 /* Save the transport */
1506 pjsua_var.tpdata[id].type = type;
1507 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1508 pjsua_var.tpdata[id].data.factory = tcp;
1509
Benny Prijono3569c0d2007-04-06 10:29:20 +00001510#endif /* PJ_HAS_TCP */
1511
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001512#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1513 } else if (type == PJSIP_TRANSPORT_TLS) {
1514 /*
1515 * Create TLS transport.
1516 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001517 /*
1518 * Create TCP transport.
1519 */
1520 pjsua_transport_config config;
1521 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001522 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001523 pj_sockaddr_in local_addr;
1524
1525 /* Supply default config if it's not specified */
1526 if (cfg == NULL) {
1527 pjsua_transport_config_default(&config);
1528 config.port = 5061;
1529 cfg = &config;
1530 }
1531
1532 /* Init local address */
1533 pj_sockaddr_in_init(&local_addr, 0, 0);
1534
1535 if (cfg->port)
1536 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1537
1538 if (cfg->bound_addr.slen) {
1539 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1540 if (status != PJ_SUCCESS) {
1541 pjsua_perror(THIS_FILE,
1542 "Unable to resolve transport bound address",
1543 status);
1544 goto on_return;
1545 }
1546 }
1547
1548 /* Init published name */
1549 pj_bzero(&a_name, sizeof(pjsip_host_port));
1550 if (cfg->public_addr.slen)
1551 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001552
1553 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001554 &cfg->tls_setting,
1555 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001556 if (status != PJ_SUCCESS) {
1557 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1558 status);
1559 goto on_return;
1560 }
1561
1562 /* Save the transport */
1563 pjsua_var.tpdata[id].type = type;
1564 pjsua_var.tpdata[id].local_name = tls->addr_name;
1565 pjsua_var.tpdata[id].data.factory = tls;
1566#endif
1567
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001568 } else {
1569 status = PJSIP_EUNSUPTRANSPORT;
1570 pjsua_perror(THIS_FILE, "Error creating transport", status);
1571 goto on_return;
1572 }
1573
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001574
1575 /* Return the ID */
1576 if (p_id) *p_id = id;
1577
1578 status = PJ_SUCCESS;
1579
1580on_return:
1581
1582 PJSUA_UNLOCK();
1583
Benny Prijonod8410532006-06-15 11:04:33 +00001584 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001585}
1586
1587
1588/*
1589 * Register transport that has been created by application.
1590 */
1591PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1592 pjsua_transport_id *p_id)
1593{
1594 unsigned id;
1595
1596 PJSUA_LOCK();
1597
1598 /* Find empty transport slot */
1599 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001600 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001601 break;
1602 }
1603
1604 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1605 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1606 PJSUA_UNLOCK();
1607 return PJ_ETOOMANY;
1608 }
1609
1610 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001611 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001612 pjsua_var.tpdata[id].local_name = tp->local_name;
1613 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001614
1615 /* Return the ID */
1616 if (p_id) *p_id = id;
1617
1618 PJSUA_UNLOCK();
1619
1620 return PJ_SUCCESS;
1621}
1622
1623
1624/*
1625 * Enumerate all transports currently created in the system.
1626 */
1627PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1628 unsigned *p_count )
1629{
1630 unsigned i, count;
1631
1632 PJSUA_LOCK();
1633
1634 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1635 ++i)
1636 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001637 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001638 continue;
1639
1640 id[count++] = i;
1641 }
1642
1643 *p_count = count;
1644
1645 PJSUA_UNLOCK();
1646
1647 return PJ_SUCCESS;
1648}
1649
1650
1651/*
1652 * Get information about transports.
1653 */
1654PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1655 pjsua_transport_info *info)
1656{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001657 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001658 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001659
Benny Prijonoac623b32006-07-03 15:19:31 +00001660 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001661
1662 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001663 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1664 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001665
1666 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001667 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001668
1669 PJSUA_LOCK();
1670
Benny Prijonoe93e2872006-06-28 16:46:49 +00001671 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1672
1673 pjsip_transport *tp = t->data.tp;
1674
1675 if (tp == NULL) {
1676 PJSUA_UNLOCK();
1677 return PJ_EINVALIDOP;
1678 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001679
Benny Prijonoe93e2872006-06-28 16:46:49 +00001680 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001681 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001682 info->type_name = pj_str(tp->type_name);
1683 info->info = pj_str(tp->info);
1684 info->flag = tp->flag;
1685 info->addr_len = tp->addr_len;
1686 info->local_addr = tp->local_addr;
1687 info->local_name = tp->local_name;
1688 info->usage_count = pj_atomic_get(tp->ref_cnt);
1689
Benny Prijono0a5cad82006-09-26 13:21:02 +00001690 status = PJ_SUCCESS;
1691
Benny Prijonoe93e2872006-06-28 16:46:49 +00001692 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1693
1694 pjsip_tpfactory *factory = t->data.factory;
1695
1696 if (factory == NULL) {
1697 PJSUA_UNLOCK();
1698 return PJ_EINVALIDOP;
1699 }
1700
1701 info->id = id;
1702 info->type = t->type;
1703 info->type_name = pj_str("TCP");
1704 info->info = pj_str("TCP transport");
1705 info->flag = factory->flag;
1706 info->addr_len = sizeof(factory->local_addr);
1707 info->local_addr = factory->local_addr;
1708 info->local_name = factory->addr_name;
1709 info->usage_count = 0;
1710
Benny Prijono0a5cad82006-09-26 13:21:02 +00001711 status = PJ_SUCCESS;
1712
1713 } else {
1714 pj_assert(!"Unsupported transport");
1715 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001716 }
1717
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001718
1719 PJSUA_UNLOCK();
1720
Benny Prijono0a5cad82006-09-26 13:21:02 +00001721 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001722}
1723
1724
1725/*
1726 * Disable a transport or re-enable it.
1727 */
1728PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1729 pj_bool_t enabled)
1730{
1731 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001732 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1733 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001734
1735 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001736 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001737
1738
1739 /* To be done!! */
1740 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001741 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001742
1743 return PJ_EINVALIDOP;
1744}
1745
1746
1747/*
1748 * Close the transport.
1749 */
1750PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1751 pj_bool_t force )
1752{
Benny Prijono5ff61872007-02-01 03:37:11 +00001753 pj_status_t status;
1754
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001755 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001756 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1757 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001758
1759 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001760 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001761
Benny Prijono0a5cad82006-09-26 13:21:02 +00001762 /* Note: destroy() may not work if there are objects still referencing
1763 * the transport.
1764 */
1765 if (force) {
1766 switch (pjsua_var.tpdata[id].type) {
1767 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001768 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1769 if (status != PJ_SUCCESS)
1770 return status;
1771 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1772 if (status != PJ_SUCCESS)
1773 return status;
1774 break;
1775
1776 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001777 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001778 /* This will close the TCP listener, but existing TCP/TLS
1779 * connections (if any) will still linger
1780 */
1781 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1782 (pjsua_var.tpdata[id].data.factory);
1783 if (status != PJ_SUCCESS)
1784 return status;
1785
Benny Prijono0a5cad82006-09-26 13:21:02 +00001786 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001787
Benny Prijono1ebd6142006-10-19 15:48:02 +00001788 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001789 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001790 }
1791
1792 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001793 /* If force is not specified, transports will be closed at their
1794 * convenient time. However this will leak PJSUA-API transport
1795 * descriptors as PJSUA-API wouldn't know when exactly the
1796 * transport is closed thus it can't cleanup PJSUA transport
1797 * descriptor.
1798 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001799 switch (pjsua_var.tpdata[id].type) {
1800 case PJSIP_TRANSPORT_UDP:
1801 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001802 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001803 case PJSIP_TRANSPORT_TCP:
1804 return (*pjsua_var.tpdata[id].data.factory->destroy)
1805 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001806 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001807 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001808 }
1809 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001810
Benny Prijono5ff61872007-02-01 03:37:11 +00001811 /* Cleanup pjsua data when force is applied */
1812 if (force) {
1813 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1814 pjsua_var.tpdata[id].data.ptr = NULL;
1815 }
1816
1817 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001818}
1819
1820
1821/*
1822 * Add additional headers etc in msg_data specified by application
1823 * when sending requests.
1824 */
1825void pjsua_process_msg_data(pjsip_tx_data *tdata,
1826 const pjsua_msg_data *msg_data)
1827{
1828 pj_bool_t allow_body;
1829 const pjsip_hdr *hdr;
1830
Benny Prijono56315612006-07-18 14:39:40 +00001831 /* Always add User-Agent */
1832 if (pjsua_var.ua_cfg.user_agent.slen &&
1833 tdata->msg->type == PJSIP_REQUEST_MSG)
1834 {
1835 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1836 pjsip_hdr *h;
1837 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1838 &STR_USER_AGENT,
1839 &pjsua_var.ua_cfg.user_agent);
1840 pjsip_msg_add_hdr(tdata->msg, h);
1841 }
1842
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001843 if (!msg_data)
1844 return;
1845
1846 hdr = msg_data->hdr_list.next;
1847 while (hdr && hdr != &msg_data->hdr_list) {
1848 pjsip_hdr *new_hdr;
1849
Benny Prijonoa1e69682007-05-11 15:14:34 +00001850 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001851 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1852
1853 hdr = hdr->next;
1854 }
1855
1856 allow_body = (tdata->msg->body == NULL);
1857
1858 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1859 pjsip_media_type ctype;
1860 pjsip_msg_body *body;
1861
1862 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1863 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1864 &msg_data->msg_body);
1865 tdata->msg->body = body;
1866 }
1867}
1868
1869
1870/*
1871 * Add route_set to outgoing requests
1872 */
1873void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1874 const pjsip_route_hdr *route_set )
1875{
1876 const pjsip_route_hdr *r;
1877
1878 r = route_set->next;
1879 while (r != route_set) {
1880 pjsip_route_hdr *new_r;
1881
Benny Prijonoa1e69682007-05-11 15:14:34 +00001882 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001883 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1884
1885 r = r->next;
1886 }
1887}
1888
1889
1890/*
1891 * Simple version of MIME type parsing (it doesn't support parameters)
1892 */
1893void pjsua_parse_media_type( pj_pool_t *pool,
1894 const pj_str_t *mime,
1895 pjsip_media_type *media_type)
1896{
1897 pj_str_t tmp;
1898 char *pos;
1899
Benny Prijonoac623b32006-07-03 15:19:31 +00001900 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001901
1902 pj_strdup_with_null(pool, &tmp, mime);
1903
1904 pos = pj_strchr(&tmp, '/');
1905 if (pos) {
1906 media_type->type.ptr = tmp.ptr;
1907 media_type->type.slen = (pos-tmp.ptr);
1908 media_type->subtype.ptr = pos+1;
1909 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1910 } else {
1911 media_type->type = tmp;
1912 }
1913}
1914
1915
1916/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001917 * Internal function to init transport selector from transport id.
1918 */
1919void pjsua_init_tpselector(pjsua_transport_id tp_id,
1920 pjsip_tpselector *sel)
1921{
1922 pjsua_transport_data *tpdata;
1923 unsigned flag;
1924
1925 pj_bzero(sel, sizeof(*sel));
1926 if (tp_id == PJSUA_INVALID_ID)
1927 return;
1928
Benny Prijonoa1e69682007-05-11 15:14:34 +00001929 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001930 tpdata = &pjsua_var.tpdata[tp_id];
1931
1932 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1933
1934 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1935 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1936 sel->u.transport = tpdata->data.tp;
1937 } else {
1938 sel->type = PJSIP_TPSELECTOR_LISTENER;
1939 sel->u.listener = tpdata->data.factory;
1940 }
1941}
1942
1943
Benny Prijono6ba8c542007-10-16 01:34:14 +00001944/* Callback upon NAT detection completion */
1945static void nat_detect_cb(void *user_data,
1946 const pj_stun_nat_detect_result *res)
1947{
1948 PJ_UNUSED_ARG(user_data);
1949
1950 pjsua_var.nat_in_progress = PJ_FALSE;
1951 pjsua_var.nat_status = res->status;
1952 pjsua_var.nat_type = res->nat_type;
1953
1954 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
1955 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
1956 }
1957}
1958
1959
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001960/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001961 * Detect NAT type.
1962 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00001963PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001964{
1965 pj_status_t status;
1966
Benny Prijono6ba8c542007-10-16 01:34:14 +00001967 if (pjsua_var.nat_in_progress)
1968 return PJ_SUCCESS;
1969
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001970 /* Make sure STUN server resolution has completed */
1971 status = pjsua_resolve_stun_server(PJ_TRUE);
1972 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001973 pjsua_var.nat_status = status;
1974 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001975 return status;
1976 }
1977
1978 /* Make sure we have STUN */
1979 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001980 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
1981 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001982 }
1983
Benny Prijono6ba8c542007-10-16 01:34:14 +00001984 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
1985 &pjsua_var.stun_cfg,
1986 NULL, &nat_detect_cb);
1987
1988 if (status != PJ_SUCCESS) {
1989 pjsua_var.nat_status = status;
1990 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
1991 return status;
1992 }
1993
1994 pjsua_var.nat_in_progress = PJ_TRUE;
1995
1996 return PJ_SUCCESS;
1997}
1998
1999
2000/*
2001 * Get NAT type.
2002 */
2003PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
2004{
2005 *type = pjsua_var.nat_type;
2006 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002007}
2008
2009
2010/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002011 * Verify that valid SIP url is given.
2012 */
2013PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
2014{
2015 pjsip_uri *p;
2016 pj_pool_t *pool;
2017 char *url;
2018 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2019
2020 if (!len) return -1;
2021
2022 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
2023 if (!pool) return -1;
2024
Benny Prijonoa1e69682007-05-11 15:14:34 +00002025 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002026 pj_ansi_strcpy(url, c_url);
2027
2028 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002029 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
2030 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
2031 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002032 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002033 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002034
2035 pj_pool_release(pool);
2036 return p ? 0 : -1;
2037}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002038
2039
2040/*
2041 * This is a utility function to dump the stack states to log, using
2042 * verbosity level 3.
2043 */
2044PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2045{
2046 unsigned old_decor;
2047 unsigned i;
2048 char buf[1024];
2049
2050 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2051
2052 old_decor = pj_log_get_decor();
2053 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2054
2055 if (detail)
2056 pj_dump_config();
2057
2058 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2059
2060 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2061
2062 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2063 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2064 pjsua_call *call = &pjsua_var.calls[i];
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002065 pjmedia_transport_info tpinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002066 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002067
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002068 /* MSVC complains about tpinfo not being initialized */
Benny Prijono734fc2d2008-03-17 16:05:35 +00002069 //pj_bzero(&tpinfo, sizeof(tpinfo));
Benny Prijono4f093d22007-04-09 08:54:32 +00002070
Benny Prijono734fc2d2008-03-17 16:05:35 +00002071 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002072 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002073
Benny Prijono5186eae2007-12-03 14:38:25 +00002074 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002075 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002076 pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, addr_buf,
Benny Prijono5186eae2007-12-03 14:38:25 +00002077 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002078 }
2079
2080 pjsip_tsx_layer_dump(detail);
2081 pjsip_ua_dump(detail);
2082
2083
2084 /* Dump all invite sessions: */
2085 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2086
2087 if (pjsua_call_get_count() == 0) {
2088
2089 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2090
2091 } else {
2092 unsigned i;
2093
2094 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2095 if (pjsua_call_is_active(i)) {
2096 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
2097 PJ_LOG(3,(THIS_FILE, "%s", buf));
2098 }
2099 }
2100 }
2101
2102 /* Dump presence status */
2103 pjsua_pres_dump(detail);
2104
2105 pj_log_set_decor(old_decor);
2106 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2107}
2108