blob: bc26f62e9f97f8c48e6cb5abcadbfe747410ed30 [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijono32177c02008-06-20 22:44:47 +00003 * Copyright (C) 2003-2008 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);
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000116}
117
118PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
119{
120 pj_bzero(msg_data, sizeof(*msg_data));
121 pj_list_init(&msg_data->hdr_list);
122}
123
124PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
125{
126 pj_bzero(cfg, sizeof(*cfg));
127 pjsip_tls_setting_default(&cfg->tls_setting);
128}
129
130PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
131 pjsua_transport_config *dst,
132 const pjsua_transport_config *src)
133{
134 PJ_UNUSED_ARG(pool);
135 pj_memcpy(dst, src, sizeof(*src));
136}
137
138PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
139{
140 pj_bzero(cfg, sizeof(*cfg));
141
142 cfg->reg_timeout = PJSUA_REG_INTERVAL;
143 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000144 cfg->allow_contact_rewrite = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000145 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000146 cfg->ka_interval = 15;
147 cfg->ka_data = pj_str("\r\n");
Benny Prijonod8179652008-01-23 20:39:07 +0000148#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
149 cfg->use_srtp = pjsua_var.ua_cfg.use_srtp;
150 cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling;
151#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000152}
153
154PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
155{
156 pj_bzero(cfg, sizeof(*cfg));
157}
158
159PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
160{
161 pj_bzero(cfg, sizeof(*cfg));
162
163 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono50f19b32008-03-11 13:15:43 +0000164 cfg->snd_clock_rate = 0;
Benny Prijono7d60d052008-03-29 12:24:20 +0000165 cfg->channel_count = 1;
Benny Prijono37c710b2008-01-10 12:09:26 +0000166 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
167 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000168 cfg->has_ioqueue = PJ_TRUE;
169 cfg->thread_cnt = 1;
170 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
171 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
172 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
173 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
Nanang Izzuddin68559c32008-06-13 17:01:46 +0000174 cfg->snd_auto_close_time = -1;
Benny Prijonof76e1392008-06-06 14:51:48 +0000175
176 cfg->turn_conn_type = PJ_TURN_TP_UDP;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000177}
178
Benny Prijonodc39fe82006-05-26 12:17:46 +0000179
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000180/*****************************************************************************
181 * This is a very simple PJSIP module, whose sole purpose is to display
182 * incoming and outgoing messages to log. This module will have priority
183 * higher than transport layer, which means:
184 *
185 * - incoming messages will come to this module first before reaching
186 * transaction layer.
187 *
188 * - outgoing messages will come to this module last, after the message
189 * has been 'printed' to contiguous buffer by transport layer and
190 * appropriate transport instance has been decided for this message.
191 *
192 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000193
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000194/* Notification on incoming messages */
195static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
196{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000197 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
198 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000199 "--end msg--",
200 rdata->msg_info.len,
201 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000202 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000203 rdata->pkt_info.src_name,
204 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000205 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000206 rdata->msg_info.msg_buf));
207
208 /* Always return false, otherwise messages will not get processed! */
209 return PJ_FALSE;
210}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000211
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000212/* Notification on outgoing messages */
213static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
214{
215
216 /* Important note:
217 * tp_info field is only valid after outgoing messages has passed
218 * transport layer. So don't try to access tp_info when the module
219 * has lower priority than transport layer.
220 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000221
Benny Prijonob4a17c92006-07-10 14:40:21 +0000222 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
223 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000224 "--end msg--",
225 (tdata->buf.cur - tdata->buf.start),
226 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000227 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000228 tdata->tp_info.dst_name,
229 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000230 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000231 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000232
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000234 return PJ_SUCCESS;
235}
236
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237/* The module instance. */
238static pjsip_module pjsua_msg_logger =
239{
240 NULL, NULL, /* prev, next. */
241 { "mod-pjsua-log", 13 }, /* Name. */
242 -1, /* Id */
243 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
244 NULL, /* load() */
245 NULL, /* start() */
246 NULL, /* stop() */
247 NULL, /* unload() */
248 &logging_on_rx_msg, /* on_rx_request() */
249 &logging_on_rx_msg, /* on_rx_response() */
250 &logging_on_tx_msg, /* on_tx_request. */
251 &logging_on_tx_msg, /* on_tx_response() */
252 NULL, /* on_tsx_state() */
253
254};
255
256
257/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000258 * Another simple module to handle incoming OPTIONS request
259 */
260
261/* Notification on incoming request */
262static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
263{
264 pjsip_tx_data *tdata;
265 pjsip_response_addr res_addr;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000266 pjmedia_transport_info tpinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000267 pjmedia_sdp_session *sdp;
268 const pjsip_hdr *cap_hdr;
269 pj_status_t status;
270
271 /* Only want to handle OPTIONS requests */
272 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000273 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000274 {
275 return PJ_FALSE;
276 }
277
278 /* Create basic response. */
279 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
280 &tdata);
281 if (status != PJ_SUCCESS) {
282 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
283 return PJ_TRUE;
284 }
285
286 /* Add Allow header */
287 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
288 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000289 pjsip_msg_add_hdr(tdata->msg,
290 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000291 }
292
293 /* Add Accept header */
294 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
295 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000296 pjsip_msg_add_hdr(tdata->msg,
297 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000298 }
299
300 /* Add Supported header */
301 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
302 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000303 pjsip_msg_add_hdr(tdata->msg,
304 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000305 }
306
307 /* Add Allow-Events header from the evsub module */
308 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
309 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000310 pjsip_msg_add_hdr(tdata->msg,
311 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000312 }
313
314 /* Add User-Agent header */
315 if (pjsua_var.ua_cfg.user_agent.slen) {
316 const pj_str_t USER_AGENT = { "User-Agent", 10};
317 pjsip_hdr *h;
318
319 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
320 &USER_AGENT,
321 &pjsua_var.ua_cfg.user_agent);
322 pjsip_msg_add_hdr(tdata->msg, h);
323 }
324
Benny Prijono617c5bc2007-04-02 19:51:21 +0000325 /* Get media socket info */
Benny Prijono734fc2d2008-03-17 16:05:35 +0000326 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000327 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000328
Benny Prijono56315612006-07-18 14:39:40 +0000329 /* Add SDP body, using call0's RTP address */
330 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000331 &tpinfo.sock_info, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000332 if (status == PJ_SUCCESS) {
333 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
334 }
335
336 /* Send response statelessly */
337 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
338 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
339 if (status != PJ_SUCCESS)
340 pjsip_tx_data_dec_ref(tdata);
341
342 return PJ_TRUE;
343}
344
345
346/* The module instance. */
347static pjsip_module pjsua_options_handler =
348{
349 NULL, NULL, /* prev, next. */
350 { "mod-pjsua-options", 17 }, /* Name. */
351 -1, /* Id */
352 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
353 NULL, /* load() */
354 NULL, /* start() */
355 NULL, /* stop() */
356 NULL, /* unload() */
357 &options_on_rx_request, /* on_rx_request() */
358 NULL, /* on_rx_response() */
359 NULL, /* on_tx_request. */
360 NULL, /* on_tx_response() */
361 NULL, /* on_tsx_state() */
362
363};
364
365
366/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000367 * These two functions are the main callbacks registered to PJSIP stack
368 * to receive SIP request and response messages that are outside any
369 * dialogs and any transactions.
370 */
Benny Prijono268ca612006-02-07 12:34:11 +0000371
372/*
373 * Handler for receiving incoming requests.
374 *
375 * This handler serves multiple purposes:
376 * - it receives requests outside dialogs.
377 * - it receives requests inside dialogs, when the requests are
378 * unhandled by other dialog usages. Example of these
379 * requests are: MESSAGE.
380 */
381static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
382{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000383 pj_bool_t processed = PJ_FALSE;
384
385 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000386
Benny Prijono84126ab2006-02-09 09:30:09 +0000387 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000388
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000389 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000390 }
391
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392 PJSUA_UNLOCK();
393
394 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000395}
396
397
398/*
399 * Handler for receiving incoming responses.
400 *
401 * This handler serves multiple purposes:
402 * - it receives strayed responses (i.e. outside any dialog and
403 * outside any transactions).
404 * - it receives responses coming to a transaction, when pjsua
405 * module is set as transaction user for the transaction.
406 * - it receives responses inside a dialog, when these responses
407 * are unhandled by other dialog usages.
408 */
409static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
410{
411 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000412 return PJ_FALSE;
413}
414
415
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000416/*****************************************************************************
417 * Logging.
418 */
419
420/* Log callback */
421static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000422{
Benny Prijono572d4852006-11-23 21:50:02 +0000423 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000424
425 if (pjsua_var.log_file) {
426 pj_ssize_t size = len;
427 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000428 /* This will slow things down considerably! Don't do it!
429 pj_file_flush(pjsua_var.log_file);
430 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000431 }
432
Benny Prijono572d4852006-11-23 21:50:02 +0000433 if (level <= (int)pjsua_var.log_cfg.console_level) {
434 if (pjsua_var.log_cfg.cb)
435 (*pjsua_var.log_cfg.cb)(level, buffer, len);
436 else
437 pj_log_write(level, buffer, len);
438 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000439}
440
441
442/*
443 * Application can call this function at any time (after pjsua_create(), of
444 * course) to change logging settings.
445 */
446PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
447{
448 pj_status_t status;
449
450 /* Save config. */
451 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
452
453 /* Redirect log function to ours */
454 pj_log_set_log_func( &log_writer );
455
Benny Prijono9cb09a22007-08-30 09:35:10 +0000456 /* Set decor */
457 pj_log_set_decor(pjsua_var.log_cfg.decor);
458
Benny Prijono4190cf92008-01-18 13:25:05 +0000459 /* Set log level */
460 pj_log_set_level(pjsua_var.log_cfg.level);
461
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000462 /* Close existing file, if any */
463 if (pjsua_var.log_file) {
464 pj_file_close(pjsua_var.log_file);
465 pjsua_var.log_file = NULL;
466 }
467
468 /* If output log file is desired, create the file: */
469 if (pjsua_var.log_cfg.log_filename.slen) {
470
471 status = pj_file_open(pjsua_var.pool,
472 pjsua_var.log_cfg.log_filename.ptr,
473 PJ_O_WRONLY,
474 &pjsua_var.log_file);
475
476 if (status != PJ_SUCCESS) {
477 pjsua_perror(THIS_FILE, "Error creating log file", status);
478 return status;
479 }
480 }
481
482 /* Unregister msg logging if it's previously registered */
483 if (pjsua_msg_logger.id >= 0) {
484 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
485 pjsua_msg_logger.id = -1;
486 }
487
488 /* Enable SIP message logging */
489 if (pjsua_var.log_cfg.msg_logging)
490 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
491
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000492 return PJ_SUCCESS;
493}
494
495
496/*****************************************************************************
497 * PJSUA Base API.
498 */
499
500/* Worker thread function. */
501static int worker_thread(void *arg)
502{
503 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000504
Benny Prijono268ca612006-02-07 12:34:11 +0000505 PJ_UNUSED_ARG(arg);
506
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000507 while (!pjsua_var.thread_quit_flag) {
508 int count;
509
510 count = pjsua_handle_events(TIMEOUT);
511 if (count < 0)
512 pj_thread_sleep(TIMEOUT);
513 }
Benny Prijono268ca612006-02-07 12:34:11 +0000514
515 return 0;
516}
517
Benny Prijonodc39fe82006-05-26 12:17:46 +0000518
Benny Prijono8389c312008-02-21 21:36:34 +0000519/* Init random seed */
520static void init_random_seed(void)
521{
522 pj_sockaddr addr;
523 const pj_str_t *hostname;
524 pj_uint32_t pid;
525 pj_time_val t;
526 unsigned seed=0;
527
528 /* Add hostname */
529 hostname = pj_gethostname();
530 seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen);
531
532 /* Add primary IP address */
533 if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS)
534 seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4);
535
536 /* Get timeofday */
537 pj_gettimeofday(&t);
538 seed = pj_hash_calc(seed, &t, sizeof(t));
539
540 /* Add PID */
541 pid = pj_getpid();
542 seed = pj_hash_calc(seed, &pid, sizeof(pid));
543
544 /* Init random seed */
545 pj_srand(seed);
546}
547
Benny Prijono268ca612006-02-07 12:34:11 +0000548/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000550 */
551PJ_DEF(pj_status_t) pjsua_create(void)
552{
553 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000554
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000555 /* Init pjsua data */
556 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000557
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000558 /* Set default logging settings */
559 pjsua_logging_config_default(&pjsua_var.log_cfg);
560
561 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000562 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000563 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
564
Benny Prijono8389c312008-02-21 21:36:34 +0000565 /* Init random seed */
566 init_random_seed();
Benny Prijono268ca612006-02-07 12:34:11 +0000567
Benny Prijonofccab712006-02-22 22:23:22 +0000568 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000569 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000570 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000571
Benny Prijonoec921342007-03-24 13:00:30 +0000572 /* Init PJNATH */
573 status = pjnath_init();
574 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000575
Benny Prijono094d3ad2006-11-21 08:41:00 +0000576 /* Set default sound device ID */
577 pjsua_var.cap_dev = pjsua_var.play_dev = -1;
578
Benny Prijono268ca612006-02-07 12:34:11 +0000579 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000580 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000581
582 /* Create memory pool for application. */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000583 pjsua_var.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000584
585 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000586
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000587 /* Create mutex */
588 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
589 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000590 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000591 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000592 return status;
593 }
594
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000595 /* Must create SIP endpoint to initialize SIP parser. The parser
596 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000597 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000598 status = pjsip_endpt_create(&pjsua_var.cp.factory,
599 pj_gethostname()->ptr,
600 &pjsua_var.endpt);
601 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000602
603
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000604 return PJ_SUCCESS;
605}
606
607
608/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000609 * Initialize pjsua with the specified settings. All the settings are
610 * optional, and the default values will be used when the config is not
611 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000612 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
614 const pjsua_logging_config *log_cfg,
615 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000616{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000617 pjsua_config default_cfg;
618 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000619 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijonodc39fe82006-05-26 12:17:46 +0000620 pj_status_t status;
621
622
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000623 /* Create default configurations when the config is not supplied */
624
625 if (ua_cfg == NULL) {
626 pjsua_config_default(&default_cfg);
627 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000628 }
629
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000630 if (media_cfg == NULL) {
631 pjsua_media_config_default(&default_media_cfg);
632 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000633 }
634
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000635 /* Initialize logging first so that info/errors can be captured */
636 if (log_cfg) {
637 status = pjsua_reconfigure_logging(log_cfg);
638 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000639 }
640
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000641 /* If nameserver is configured, create DNS resolver instance and
642 * set it to be used by SIP resolver.
643 */
644 if (ua_cfg->nameserver_count) {
645#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000646 unsigned i;
647
648 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000649 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
650 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000651 if (status != PJ_SUCCESS) {
652 pjsua_perror(THIS_FILE, "Error creating resolver", status);
653 return status;
654 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000655
656 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000657 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
658 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000659 ua_cfg->nameserver, NULL);
660 if (status != PJ_SUCCESS) {
661 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
662 return status;
663 }
664
665 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000666 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000667 if (status != PJ_SUCCESS) {
668 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
669 return status;
670 }
671
672 /* Print nameservers */
673 for (i=0; i<ua_cfg->nameserver_count; ++i) {
674 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
675 (int)ua_cfg->nameserver[i].slen,
676 ua_cfg->nameserver[i].ptr));
677 }
678#else
679 PJ_LOG(2,(THIS_FILE,
680 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
681#endif
682 }
683
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000684 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000685
686 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000687 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
688 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000689
Benny Prijonodc39fe82006-05-26 12:17:46 +0000690
691 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000692 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
693 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000694
Benny Prijonodc39fe82006-05-26 12:17:46 +0000695
Benny Prijono053f5222006-11-11 16:16:04 +0000696 /* Initialize Replaces support. */
697 status = pjsip_replaces_init_module( pjsua_var.endpt );
698 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
699
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000700 /* Initialize 100rel support */
701 status = pjsip_100rel_init_module(pjsua_var.endpt);
702 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000703
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000704 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000705 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000706 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000707 {
708 NULL, NULL, /* prev, next. */
709 { "mod-pjsua", 9 }, /* Name. */
710 -1, /* Id */
711 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
712 NULL, /* load() */
713 NULL, /* start() */
714 NULL, /* stop() */
715 NULL, /* unload() */
716 &mod_pjsua_on_rx_request, /* on_rx_request() */
717 &mod_pjsua_on_rx_response, /* on_rx_response() */
718 NULL, /* on_tx_request. */
719 NULL, /* on_tx_response() */
720 NULL, /* on_tsx_state() */
721 };
722
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000723 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000724
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000725 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
726 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000727 }
728
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000729
Benny Prijonodc39fe82006-05-26 12:17:46 +0000730
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000731 /* Initialize PJSUA call subsystem: */
732 status = pjsua_call_subsys_init(ua_cfg);
733 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000734 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000735
736
Benny Prijonoc97608e2007-03-23 16:34:20 +0000737 /* Start resolving STUN server */
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000738
Benny Prijonoc97608e2007-03-23 16:34:20 +0000739 status = pjsua_resolve_stun_server(PJ_FALSE);
740 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
741 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
742 return status;
743 }
744
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000745 /* Initialize PJSUA media subsystem */
746 status = pjsua_media_subsys_init(media_cfg);
747 if (status != PJ_SUCCESS)
748 goto on_error;
749
Benny Prijonodc39fe82006-05-26 12:17:46 +0000750
751 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000752 status = pjsip_evsub_init_module(pjsua_var.endpt);
753 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000754
Benny Prijonodc39fe82006-05-26 12:17:46 +0000755
756 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000757 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
758 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000759
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000760 /* Init PUBLISH module */
761 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000762
763 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000764 status = pjsip_xfer_init_module( pjsua_var.endpt );
765 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000766
767 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000768 status = pjsua_pres_init();
769 if (status != PJ_SUCCESS)
770 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000771
772 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000773 status = pjsua_im_init();
774 if (status != PJ_SUCCESS)
775 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000776
Benny Prijonoc8141a82006-08-20 09:12:19 +0000777 /* Register OPTIONS handler */
778 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
779
780 /* Add OPTIONS in Allow header */
781 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
782 NULL, 1, &STR_OPTIONS);
783
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000784 /* Start worker thread if needed. */
785 if (pjsua_var.ua_cfg.thread_cnt) {
786 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000787
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000788 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
789 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000790
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000791 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
792 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
793 NULL, 0, 0, &pjsua_var.thread[i]);
794 if (status != PJ_SUCCESS)
795 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000796 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000797 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
798 pjsua_var.ua_cfg.thread_cnt));
799 } else {
800 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000801 }
802
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000803 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000804
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000805 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000806 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000807
Benny Prijono268ca612006-02-07 12:34:11 +0000808 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000809
810on_error:
811 pjsua_destroy();
812 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000813}
814
815
Benny Prijono834aee32006-02-19 01:38:06 +0000816/* Sleep with polling */
817static void busy_sleep(unsigned msec)
818{
Benny Prijonob2c96822007-05-03 13:31:21 +0000819#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Benny Prijono897f9f82007-05-03 19:56:21 +0000820 /* Ideally we shouldn't call pj_thread_sleep() and rather
821 * CActiveScheduler::WaitForAnyRequest() here, but that will
822 * drag in Symbian header and it doesn't look pretty.
823 */
Benny Prijonob2c96822007-05-03 13:31:21 +0000824 pj_thread_sleep(msec);
825#else
Benny Prijono834aee32006-02-19 01:38:06 +0000826 pj_time_val timeout, now;
827
828 pj_gettimeofday(&timeout);
829 timeout.msec += msec;
830 pj_time_val_normalize(&timeout);
831
832 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000833 while (pjsua_handle_events(10) > 0)
834 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000835 pj_gettimeofday(&now);
836 } while (PJ_TIME_VAL_LT(now, timeout));
Benny Prijonob2c96822007-05-03 13:31:21 +0000837#endif
Benny Prijono834aee32006-02-19 01:38:06 +0000838}
839
Benny Prijonoebbf6892007-03-24 17:37:25 +0000840
Benny Prijonoebbf6892007-03-24 17:37:25 +0000841/*
842 * Callback function to receive notification from the resolver
843 * when the resolution process completes.
844 */
845static void stun_dns_srv_resolver_cb(void *user_data,
846 pj_status_t status,
847 const pj_dns_srv_record *rec)
848{
849 unsigned i;
850
851 PJ_UNUSED_ARG(user_data);
852
853 pjsua_var.stun_status = status;
854
855 if (status != PJ_SUCCESS) {
856 /* DNS SRV resolution failed. If stun_host is specified, resolve
857 * it with gethostbyname()
858 */
859 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000860 pj_str_t str_host, str_port;
861 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000862 pj_hostent he;
863
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000864 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
865 if (str_port.ptr != NULL) {
866 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
867 str_host.slen = (str_port.ptr - str_host.ptr);
868 str_port.ptr++;
869 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
870 str_host.slen - 1;
871 port = (int)pj_strtoul(&str_port);
872 if (port < 1 || port > 65535) {
873 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
874 pjsua_var.stun_status = PJ_EINVAL;
875 return;
876 }
877 } else {
878 str_host = pjsua_var.ua_cfg.stun_host;
879 port = 3478;
880 }
881
882 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000883
884 if (pjsua_var.stun_status == PJ_SUCCESS) {
885 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
886 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000887 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000888
889 PJ_LOG(3,(THIS_FILE,
890 "STUN server %.*s resolved, address is %s:%d",
891 (int)pjsua_var.ua_cfg.stun_host.slen,
892 pjsua_var.ua_cfg.stun_host.ptr,
893 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
894 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
895 }
896 } else {
897 char errmsg[PJ_ERR_MSG_SIZE];
898
899 pj_strerror(status, errmsg, sizeof(errmsg));
900 PJ_LOG(1,(THIS_FILE,
901 "DNS SRV resolution failed for STUN server %.*s: %s",
902 (int)pjsua_var.ua_cfg.stun_domain.slen,
903 pjsua_var.ua_cfg.stun_domain.ptr,
904 errmsg));
905 }
906 return;
907 }
908
Benny Prijonof5afa922007-06-11 16:51:18 +0000909 pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0);
910 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL,
911 rec->entry[0].port);
912 pjsua_var.stun_srv.ipv4.sin_addr.s_addr =
913 rec->entry[0].server.addr[0].s_addr;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000914
915 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
916 (int)pjsua_var.ua_cfg.stun_domain.slen,
917 pjsua_var.ua_cfg.stun_domain.ptr,
918 rec->count));
919
920 for (i=0; i<rec->count; ++i) {
921 PJ_LOG(3,(THIS_FILE,
922 " %d: prio=%d, weight=%d %s:%d",
923 i, rec->entry[i].priority, rec->entry[i].weight,
Benny Prijonof5afa922007-06-11 16:51:18 +0000924 pj_inet_ntoa(rec->entry[i].server.addr[0]),
925 (int)rec->entry[i].port));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000926 }
927
928}
929
Benny Prijono268ca612006-02-07 12:34:11 +0000930/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000931 * Resolve STUN server.
932 */
933pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
934{
935 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
936 /* Initialize STUN configuration */
937 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
938 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
939 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
940
941 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000942
943 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000944
Benny Prijonoebbf6892007-03-24 17:37:25 +0000945 /* If stun_domain is specified, resolve STUN servers with DNS
946 * SRV resolution.
947 */
948 if (pjsua_var.ua_cfg.stun_domain.slen) {
949 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000950 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000951
952 /* Fail if resolver is not configured */
953 if (pjsua_var.resolver == NULL) {
954 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
955 "stun_domain is specified"));
956 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
957 return PJLIB_UTIL_EDNSNONS;
958 }
959 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000960 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000961 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
962 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonof5afa922007-06-11 16:51:18 +0000963 0, NULL, &stun_dns_srv_resolver_cb, NULL);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000964 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000965 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
966 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000967 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000968 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000969 } else {
970 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000971 }
972 }
973 /* Otherwise if stun_host is specified, resolve STUN server with
974 * gethostbyname().
975 */
976 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000977 pj_str_t str_host, str_port;
978 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000979 pj_hostent he;
980
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000981 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
982 if (str_port.ptr != NULL) {
983 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
984 str_host.slen = (str_port.ptr - str_host.ptr);
985 str_port.ptr++;
986 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
987 str_host.slen - 1;
988 port = (int)pj_strtoul(&str_port);
989 if (port < 1 || port > 65535) {
990 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
991 pjsua_var.stun_status = PJ_EINVAL;
992 return pjsua_var.stun_status;
993 }
994 } else {
995 str_host = pjsua_var.ua_cfg.stun_host;
996 port = 3478;
997 }
998
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000999 pjsua_var.stun_status =
1000 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, &str_host,
1001 (pj_uint16_t)port);
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001002
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001003 if (pjsua_var.stun_status != PJ_SUCCESS) {
1004 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001005
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001006 if (pjsua_var.stun_status == PJ_SUCCESS) {
1007 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
1008 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
1009 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
1010 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001011 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001012
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001013 PJ_LOG(3,(THIS_FILE,
1014 "STUN server %.*s resolved, address is %s:%d",
1015 (int)pjsua_var.ua_cfg.stun_host.slen,
1016 pjsua_var.ua_cfg.stun_host.ptr,
1017 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
1018 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
1019
Benny Prijonoc97608e2007-03-23 16:34:20 +00001020 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001021 /* Otherwise disable STUN. */
1022 else {
1023 pjsua_var.stun_status = PJ_SUCCESS;
1024 }
1025
1026
Benny Prijonoc97608e2007-03-23 16:34:20 +00001027 return pjsua_var.stun_status;
1028
1029 } else if (pjsua_var.stun_status == PJ_EPENDING) {
1030 /* STUN server resolution has been started, wait for the
1031 * result.
1032 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001033 if (wait) {
1034 while (pjsua_var.stun_status == PJ_EPENDING)
1035 pjsua_handle_events(10);
1036 }
1037
1038 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001039
1040 } else {
1041 /* STUN server has been resolved, return the status */
1042 return pjsua_var.stun_status;
1043 }
1044}
1045
1046/*
Benny Prijono268ca612006-02-07 12:34:11 +00001047 * Destroy pjsua.
1048 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001049PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001050{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001051 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001052
1053 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001054 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001055
1056 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001057 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1058 if (pjsua_var.thread[i]) {
1059 pj_thread_join(pjsua_var.thread[i]);
1060 pj_thread_destroy(pjsua_var.thread[i]);
1061 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001062 }
Benny Prijono268ca612006-02-07 12:34:11 +00001063 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001064
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001065 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001066 /* Terminate all calls. */
1067 pjsua_call_hangup_all();
1068
1069 /* Terminate all presence subscriptions. */
1070 pjsua_pres_shutdown();
1071
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001072 /* Destroy pool in the buddy object */
1073 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1074 if (pjsua_var.buddy[i].pool) {
1075 pj_pool_release(pjsua_var.buddy[i].pool);
1076 pjsua_var.buddy[i].pool = NULL;
1077 }
1078 }
1079
1080 /* Destroy accounts */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001081 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1082 if (!pjsua_var.acc[i].valid)
1083 continue;
1084
1085 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001086 pjsua_acc_set_registration(i, PJ_FALSE);
1087 }
Benny Prijonoc91ed8d2008-07-13 12:24:55 +00001088
1089 if (pjsua_var.acc[i].pool) {
1090 pj_pool_release(pjsua_var.acc[i].pool);
1091 pjsua_var.acc[i].pool = NULL;
1092 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001093 }
1094
1095 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +00001096 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1097 busy_sleep(1000);
1098 }
Benny Prijono834aee32006-02-19 01:38:06 +00001099
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001100 /* Destroy media */
1101 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001102
Benny Prijono268ca612006-02-07 12:34:11 +00001103 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001104 if (pjsua_var.endpt) {
1105 pjsip_endpt_destroy(pjsua_var.endpt);
1106 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001107 }
Benny Prijono268ca612006-02-07 12:34:11 +00001108
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001109 /* Destroy mutex */
1110 if (pjsua_var.mutex) {
1111 pj_mutex_destroy(pjsua_var.mutex);
1112 pjsua_var.mutex = NULL;
1113 }
1114
1115 /* Destroy pool and pool factory. */
1116 if (pjsua_var.pool) {
1117 pj_pool_release(pjsua_var.pool);
1118 pjsua_var.pool = NULL;
1119 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001120
1121 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1122
1123 /* End logging */
1124 if (pjsua_var.log_file) {
1125 pj_file_close(pjsua_var.log_file);
1126 pjsua_var.log_file = NULL;
1127 }
1128
1129 /* Shutdown PJLIB */
1130 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001131 }
Benny Prijono268ca612006-02-07 12:34:11 +00001132
Benny Prijonof762ee72006-12-01 11:14:37 +00001133 /* Clear pjsua_var */
1134 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1135
Benny Prijono268ca612006-02-07 12:34:11 +00001136 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001137 return PJ_SUCCESS;
1138}
1139
1140
1141/**
1142 * Application is recommended to call this function after all initialization
1143 * is done, so that the library can do additional checking set up
1144 * additional
1145 *
1146 * @return PJ_SUCCESS on success, or the appropriate error code.
1147 */
1148PJ_DEF(pj_status_t) pjsua_start(void)
1149{
1150 pj_status_t status;
1151
1152 status = pjsua_call_subsys_start();
1153 if (status != PJ_SUCCESS)
1154 return status;
1155
1156 status = pjsua_media_subsys_start();
1157 if (status != PJ_SUCCESS)
1158 return status;
1159
1160 status = pjsua_pres_start();
1161 if (status != PJ_SUCCESS)
1162 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001163
1164 return PJ_SUCCESS;
1165}
1166
Benny Prijono9fc735d2006-05-28 14:58:12 +00001167
1168/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001169 * Poll pjsua for events, and if necessary block the caller thread for
1170 * the specified maximum interval (in miliseconds).
1171 */
1172PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1173{
Benny Prijono897f9f82007-05-03 19:56:21 +00001174#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
1175 /* Ideally we shouldn't call pj_thread_sleep() and rather
1176 * CActiveScheduler::WaitForAnyRequest() here, but that will
1177 * drag in Symbian header and it doesn't look pretty.
1178 */
1179 pj_thread_sleep(msec_timeout);
1180 return msec_timeout;
1181#else
1182
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001183 unsigned count = 0;
1184 pj_time_val tv;
1185 pj_status_t status;
1186
1187 tv.sec = 0;
1188 tv.msec = msec_timeout;
1189 pj_time_val_normalize(&tv);
1190
1191 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1192
1193 if (status != PJ_SUCCESS)
1194 return -status;
1195
1196 return count;
Benny Prijono897f9f82007-05-03 19:56:21 +00001197#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001198}
1199
1200
1201/*
1202 * Create memory pool.
1203 */
1204PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1205 pj_size_t increment)
1206{
1207 /* Pool factory is thread safe, no need to lock */
1208 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1209 NULL);
1210}
1211
1212
1213/*
1214 * Internal function to get SIP endpoint instance of pjsua, which is
1215 * needed for example to register module, create transports, etc.
1216 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001217 */
1218PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1219{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001220 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001221}
1222
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001223/*
1224 * Internal function to get media endpoint instance.
1225 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001226 */
1227PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1228{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001229 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001230}
1231
Benny Prijono97b87172006-08-24 14:25:14 +00001232/*
1233 * Internal function to get PJSUA pool factory.
1234 */
1235PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1236{
1237 return &pjsua_var.cp.factory;
1238}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001239
1240/*****************************************************************************
1241 * PJSUA SIP Transport API.
1242 */
1243
1244/*
Benny Prijono23674a32007-12-01 08:59:25 +00001245 * Tools to get address string.
1246 */
1247static const char *addr_string(const pj_sockaddr_t *addr)
1248{
1249 static char str[128];
1250 str[0] = '\0';
1251 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1252 pj_sockaddr_get_addr(addr),
1253 str, sizeof(str));
1254 return str;
1255}
1256
1257/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001258 * Create and initialize SIP socket (and possibly resolve public
1259 * address via STUN, depending on config).
1260 */
Benny Prijono23674a32007-12-01 08:59:25 +00001261static pj_status_t create_sip_udp_sock(int af,
1262 const pj_str_t *bind_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001263 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001264 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001265 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001266{
Benny Prijono23674a32007-12-01 08:59:25 +00001267 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijonoc97608e2007-03-23 16:34:20 +00001268 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001269 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001270 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001271 pj_status_t status;
1272
Benny Prijonoc97608e2007-03-23 16:34:20 +00001273 /* Make sure STUN server resolution has completed */
1274 status = pjsua_resolve_stun_server(PJ_TRUE);
1275 if (status != PJ_SUCCESS) {
1276 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1277 return status;
1278 }
1279
Benny Prijono23674a32007-12-01 08:59:25 +00001280 /* Initialize bound address */
1281 if (bind_param->slen) {
1282 status = pj_sockaddr_init(af, &bind_addr, bind_param,
1283 (pj_uint16_t)port);
1284 if (status != PJ_SUCCESS) {
1285 pjsua_perror(THIS_FILE,
1286 "Unable to resolve transport bound address",
1287 status);
1288 return status;
1289 }
1290 } else {
1291 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1292 }
1293
1294 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001295 if (status != PJ_SUCCESS) {
1296 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001297 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001298 }
1299
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001300 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001301 if (status != PJ_SUCCESS) {
1302 pjsua_perror(THIS_FILE, "bind() error", status);
1303 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001304 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001305 }
1306
Benny Prijonoe347cb02007-02-14 14:36:13 +00001307 /* If port is zero, get the bound port */
1308 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001309 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001310 int namelen = sizeof(bound_addr);
1311 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1312 if (status != PJ_SUCCESS) {
1313 pjsua_perror(THIS_FILE, "getsockname() error", status);
1314 pj_sock_close(sock);
1315 return status;
1316 }
1317
Benny Prijono23674a32007-12-01 08:59:25 +00001318 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001319 }
1320
Benny Prijonoc97608e2007-03-23 16:34:20 +00001321 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001322 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1323 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001324 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001325 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001326 }
1327
1328 /* Get the published address, either by STUN or by resolving
1329 * the name of local host.
1330 */
Benny Prijono23674a32007-12-01 08:59:25 +00001331 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001332 /*
1333 * Public address is already specified, no need to resolve the
1334 * address, only set the port.
1335 */
Benny Prijono23674a32007-12-01 08:59:25 +00001336 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1337 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001338
1339 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001340 /*
1341 * STUN is specified, resolve the address with STUN.
1342 */
Benny Prijono23674a32007-12-01 08:59:25 +00001343 if (af != pj_AF_INET()) {
1344 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1345 pj_sock_close(sock);
1346 return PJ_EAFNOTSUP;
1347 }
1348
Benny Prijono14c2b862007-02-21 00:40:05 +00001349 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001350 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1351 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001352 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001353 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001354 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001355 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001356 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001357 }
1358
1359 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001360 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001361
Benny Prijono42d08d22007-12-20 11:23:07 +00001362 if (pj_sockaddr_has_addr(&bind_addr)) {
1363 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1364 } else {
1365 status = pj_gethostip(af, p_pub_addr);
1366 if (status != PJ_SUCCESS) {
1367 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1368 pj_sock_close(sock);
1369 return status;
1370 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001371 }
1372
Benny Prijono23674a32007-12-01 08:59:25 +00001373 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1374 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001375 }
1376
1377 *p_sock = sock;
1378
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001379 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001380 addr_string(p_pub_addr),
1381 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001382
Benny Prijonod8410532006-06-15 11:04:33 +00001383 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001384}
1385
1386
1387/*
1388 * Create SIP transport.
1389 */
1390PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1391 const pjsua_transport_config *cfg,
1392 pjsua_transport_id *p_id)
1393{
1394 pjsip_transport *tp;
1395 unsigned id;
1396 pj_status_t status;
1397
1398 PJSUA_LOCK();
1399
1400 /* Find empty transport slot */
1401 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001402 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001403 break;
1404 }
1405
1406 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1407 status = PJ_ETOOMANY;
1408 pjsua_perror(THIS_FILE, "Error creating transport", status);
1409 goto on_return;
1410 }
1411
1412 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001413 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001414 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001415 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001416 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001417 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001418 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001419 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001420 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001421 pjsip_host_port addr_name;
1422
1423 /* Supply default config if it's not specified */
1424 if (cfg == NULL) {
1425 pjsua_transport_config_default(&config);
1426 cfg = &config;
1427 }
1428
Benny Prijono0a5cad82006-09-26 13:21:02 +00001429 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001430 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1431 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001432 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001433 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1434 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001435 if (status != PJ_SUCCESS) {
1436 pjsua_perror(THIS_FILE,
1437 "Unable to resolve transport public address",
1438 status);
1439 goto on_return;
1440 }
1441 }
1442
1443 /* Create the socket and possibly resolve the address with STUN
1444 * (only when public address is not specified).
1445 */
Benny Prijono23674a32007-12-01 08:59:25 +00001446 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
1447 &cfg->bound_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001448 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001449 if (status != PJ_SUCCESS)
1450 goto on_return;
1451
Benny Prijono23674a32007-12-01 08:59:25 +00001452 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1453 addr_name.host = pj_str(hostbuf);
1454 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001455
1456 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001457 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1458 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001459 if (status != PJ_SUCCESS) {
1460 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1461 status);
1462 pj_sock_close(sock);
1463 goto on_return;
1464 }
1465
Benny Prijonoe93e2872006-06-28 16:46:49 +00001466
1467 /* Save the transport */
1468 pjsua_var.tpdata[id].type = type;
1469 pjsua_var.tpdata[id].local_name = tp->local_name;
1470 pjsua_var.tpdata[id].data.tp = tp;
1471
Benny Prijono3569c0d2007-04-06 10:29:20 +00001472#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1473
Benny Prijonob2477142007-12-05 04:09:59 +00001474 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001475 /*
1476 * Create TCP transport.
1477 */
1478 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001479 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001480 pjsip_tpfactory *tcp;
1481 pj_sockaddr_in local_addr;
1482
1483 /* Supply default config if it's not specified */
1484 if (cfg == NULL) {
1485 pjsua_transport_config_default(&config);
1486 cfg = &config;
1487 }
1488
1489 /* Init local address */
1490 pj_sockaddr_in_init(&local_addr, 0, 0);
1491
1492 if (cfg->port)
1493 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1494
Benny Prijono0a5cad82006-09-26 13:21:02 +00001495 if (cfg->bound_addr.slen) {
1496 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1497 if (status != PJ_SUCCESS) {
1498 pjsua_perror(THIS_FILE,
1499 "Unable to resolve transport bound address",
1500 status);
1501 goto on_return;
1502 }
1503 }
1504
1505 /* Init published name */
1506 pj_bzero(&a_name, sizeof(pjsip_host_port));
1507 if (cfg->public_addr.slen)
1508 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001509
1510 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001511 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1512 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001513
1514 if (status != PJ_SUCCESS) {
1515 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1516 status);
1517 goto on_return;
1518 }
1519
1520 /* Save the transport */
1521 pjsua_var.tpdata[id].type = type;
1522 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1523 pjsua_var.tpdata[id].data.factory = tcp;
1524
Benny Prijono3569c0d2007-04-06 10:29:20 +00001525#endif /* PJ_HAS_TCP */
1526
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001527#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1528 } else if (type == PJSIP_TRANSPORT_TLS) {
1529 /*
1530 * Create TLS transport.
1531 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001532 /*
1533 * Create TCP transport.
1534 */
1535 pjsua_transport_config config;
1536 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001537 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001538 pj_sockaddr_in local_addr;
1539
1540 /* Supply default config if it's not specified */
1541 if (cfg == NULL) {
1542 pjsua_transport_config_default(&config);
1543 config.port = 5061;
1544 cfg = &config;
1545 }
1546
1547 /* Init local address */
1548 pj_sockaddr_in_init(&local_addr, 0, 0);
1549
1550 if (cfg->port)
1551 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1552
1553 if (cfg->bound_addr.slen) {
1554 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1555 if (status != PJ_SUCCESS) {
1556 pjsua_perror(THIS_FILE,
1557 "Unable to resolve transport bound address",
1558 status);
1559 goto on_return;
1560 }
1561 }
1562
1563 /* Init published name */
1564 pj_bzero(&a_name, sizeof(pjsip_host_port));
1565 if (cfg->public_addr.slen)
1566 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001567
1568 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001569 &cfg->tls_setting,
1570 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001571 if (status != PJ_SUCCESS) {
1572 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1573 status);
1574 goto on_return;
1575 }
1576
1577 /* Save the transport */
1578 pjsua_var.tpdata[id].type = type;
1579 pjsua_var.tpdata[id].local_name = tls->addr_name;
1580 pjsua_var.tpdata[id].data.factory = tls;
1581#endif
1582
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001583 } else {
1584 status = PJSIP_EUNSUPTRANSPORT;
1585 pjsua_perror(THIS_FILE, "Error creating transport", status);
1586 goto on_return;
1587 }
1588
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001589
1590 /* Return the ID */
1591 if (p_id) *p_id = id;
1592
1593 status = PJ_SUCCESS;
1594
1595on_return:
1596
1597 PJSUA_UNLOCK();
1598
Benny Prijonod8410532006-06-15 11:04:33 +00001599 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001600}
1601
1602
1603/*
1604 * Register transport that has been created by application.
1605 */
1606PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1607 pjsua_transport_id *p_id)
1608{
1609 unsigned id;
1610
1611 PJSUA_LOCK();
1612
1613 /* Find empty transport slot */
1614 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001615 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001616 break;
1617 }
1618
1619 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1620 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1621 PJSUA_UNLOCK();
1622 return PJ_ETOOMANY;
1623 }
1624
1625 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001626 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001627 pjsua_var.tpdata[id].local_name = tp->local_name;
1628 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001629
1630 /* Return the ID */
1631 if (p_id) *p_id = id;
1632
1633 PJSUA_UNLOCK();
1634
1635 return PJ_SUCCESS;
1636}
1637
1638
1639/*
1640 * Enumerate all transports currently created in the system.
1641 */
1642PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1643 unsigned *p_count )
1644{
1645 unsigned i, count;
1646
1647 PJSUA_LOCK();
1648
1649 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1650 ++i)
1651 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001652 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001653 continue;
1654
1655 id[count++] = i;
1656 }
1657
1658 *p_count = count;
1659
1660 PJSUA_UNLOCK();
1661
1662 return PJ_SUCCESS;
1663}
1664
1665
1666/*
1667 * Get information about transports.
1668 */
1669PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1670 pjsua_transport_info *info)
1671{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001672 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001673 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001674
Benny Prijonoac623b32006-07-03 15:19:31 +00001675 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001676
1677 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001678 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1679 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001680
1681 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001682 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001683
1684 PJSUA_LOCK();
1685
Benny Prijonoe93e2872006-06-28 16:46:49 +00001686 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1687
1688 pjsip_transport *tp = t->data.tp;
1689
1690 if (tp == NULL) {
1691 PJSUA_UNLOCK();
1692 return PJ_EINVALIDOP;
1693 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001694
Benny Prijonoe93e2872006-06-28 16:46:49 +00001695 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001696 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001697 info->type_name = pj_str(tp->type_name);
1698 info->info = pj_str(tp->info);
1699 info->flag = tp->flag;
1700 info->addr_len = tp->addr_len;
1701 info->local_addr = tp->local_addr;
1702 info->local_name = tp->local_name;
1703 info->usage_count = pj_atomic_get(tp->ref_cnt);
1704
Benny Prijono0a5cad82006-09-26 13:21:02 +00001705 status = PJ_SUCCESS;
1706
Benny Prijonoe93e2872006-06-28 16:46:49 +00001707 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1708
1709 pjsip_tpfactory *factory = t->data.factory;
1710
1711 if (factory == NULL) {
1712 PJSUA_UNLOCK();
1713 return PJ_EINVALIDOP;
1714 }
1715
1716 info->id = id;
1717 info->type = t->type;
1718 info->type_name = pj_str("TCP");
1719 info->info = pj_str("TCP transport");
1720 info->flag = factory->flag;
1721 info->addr_len = sizeof(factory->local_addr);
1722 info->local_addr = factory->local_addr;
1723 info->local_name = factory->addr_name;
1724 info->usage_count = 0;
1725
Benny Prijono0a5cad82006-09-26 13:21:02 +00001726 status = PJ_SUCCESS;
1727
1728 } else {
1729 pj_assert(!"Unsupported transport");
1730 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001731 }
1732
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001733
1734 PJSUA_UNLOCK();
1735
Benny Prijono0a5cad82006-09-26 13:21:02 +00001736 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001737}
1738
1739
1740/*
1741 * Disable a transport or re-enable it.
1742 */
1743PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1744 pj_bool_t enabled)
1745{
1746 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001747 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1748 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001749
1750 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001751 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001752
1753
1754 /* To be done!! */
1755 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001756 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001757
1758 return PJ_EINVALIDOP;
1759}
1760
1761
1762/*
1763 * Close the transport.
1764 */
1765PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1766 pj_bool_t force )
1767{
Benny Prijono5ff61872007-02-01 03:37:11 +00001768 pj_status_t status;
1769
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001770 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001771 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1772 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001773
1774 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001775 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001776
Benny Prijono0a5cad82006-09-26 13:21:02 +00001777 /* Note: destroy() may not work if there are objects still referencing
1778 * the transport.
1779 */
1780 if (force) {
1781 switch (pjsua_var.tpdata[id].type) {
1782 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001783 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1784 if (status != PJ_SUCCESS)
1785 return status;
1786 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1787 if (status != PJ_SUCCESS)
1788 return status;
1789 break;
1790
1791 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001792 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001793 /* This will close the TCP listener, but existing TCP/TLS
1794 * connections (if any) will still linger
1795 */
1796 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1797 (pjsua_var.tpdata[id].data.factory);
1798 if (status != PJ_SUCCESS)
1799 return status;
1800
Benny Prijono0a5cad82006-09-26 13:21:02 +00001801 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001802
Benny Prijono1ebd6142006-10-19 15:48:02 +00001803 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001804 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001805 }
1806
1807 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001808 /* If force is not specified, transports will be closed at their
1809 * convenient time. However this will leak PJSUA-API transport
1810 * descriptors as PJSUA-API wouldn't know when exactly the
1811 * transport is closed thus it can't cleanup PJSUA transport
1812 * descriptor.
1813 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001814 switch (pjsua_var.tpdata[id].type) {
1815 case PJSIP_TRANSPORT_UDP:
1816 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001817 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001818 case PJSIP_TRANSPORT_TCP:
1819 return (*pjsua_var.tpdata[id].data.factory->destroy)
1820 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001821 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001822 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001823 }
1824 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001825
Benny Prijono5ff61872007-02-01 03:37:11 +00001826 /* Cleanup pjsua data when force is applied */
1827 if (force) {
1828 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1829 pjsua_var.tpdata[id].data.ptr = NULL;
1830 }
1831
1832 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001833}
1834
1835
1836/*
1837 * Add additional headers etc in msg_data specified by application
1838 * when sending requests.
1839 */
1840void pjsua_process_msg_data(pjsip_tx_data *tdata,
1841 const pjsua_msg_data *msg_data)
1842{
1843 pj_bool_t allow_body;
1844 const pjsip_hdr *hdr;
1845
Benny Prijono56315612006-07-18 14:39:40 +00001846 /* Always add User-Agent */
1847 if (pjsua_var.ua_cfg.user_agent.slen &&
1848 tdata->msg->type == PJSIP_REQUEST_MSG)
1849 {
1850 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1851 pjsip_hdr *h;
1852 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1853 &STR_USER_AGENT,
1854 &pjsua_var.ua_cfg.user_agent);
1855 pjsip_msg_add_hdr(tdata->msg, h);
1856 }
1857
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001858 if (!msg_data)
1859 return;
1860
1861 hdr = msg_data->hdr_list.next;
1862 while (hdr && hdr != &msg_data->hdr_list) {
1863 pjsip_hdr *new_hdr;
1864
Benny Prijonoa1e69682007-05-11 15:14:34 +00001865 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001866 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1867
1868 hdr = hdr->next;
1869 }
1870
1871 allow_body = (tdata->msg->body == NULL);
1872
1873 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1874 pjsip_media_type ctype;
1875 pjsip_msg_body *body;
1876
1877 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1878 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1879 &msg_data->msg_body);
1880 tdata->msg->body = body;
1881 }
1882}
1883
1884
1885/*
1886 * Add route_set to outgoing requests
1887 */
1888void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1889 const pjsip_route_hdr *route_set )
1890{
1891 const pjsip_route_hdr *r;
1892
1893 r = route_set->next;
1894 while (r != route_set) {
1895 pjsip_route_hdr *new_r;
1896
Benny Prijonoa1e69682007-05-11 15:14:34 +00001897 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001898 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1899
1900 r = r->next;
1901 }
1902}
1903
1904
1905/*
1906 * Simple version of MIME type parsing (it doesn't support parameters)
1907 */
1908void pjsua_parse_media_type( pj_pool_t *pool,
1909 const pj_str_t *mime,
1910 pjsip_media_type *media_type)
1911{
1912 pj_str_t tmp;
1913 char *pos;
1914
Benny Prijonoac623b32006-07-03 15:19:31 +00001915 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001916
1917 pj_strdup_with_null(pool, &tmp, mime);
1918
1919 pos = pj_strchr(&tmp, '/');
1920 if (pos) {
1921 media_type->type.ptr = tmp.ptr;
1922 media_type->type.slen = (pos-tmp.ptr);
1923 media_type->subtype.ptr = pos+1;
1924 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1925 } else {
1926 media_type->type = tmp;
1927 }
1928}
1929
1930
1931/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001932 * Internal function to init transport selector from transport id.
1933 */
1934void pjsua_init_tpselector(pjsua_transport_id tp_id,
1935 pjsip_tpselector *sel)
1936{
1937 pjsua_transport_data *tpdata;
1938 unsigned flag;
1939
1940 pj_bzero(sel, sizeof(*sel));
1941 if (tp_id == PJSUA_INVALID_ID)
1942 return;
1943
Benny Prijonoa1e69682007-05-11 15:14:34 +00001944 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001945 tpdata = &pjsua_var.tpdata[tp_id];
1946
1947 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1948
1949 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1950 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1951 sel->u.transport = tpdata->data.tp;
1952 } else {
1953 sel->type = PJSIP_TPSELECTOR_LISTENER;
1954 sel->u.listener = tpdata->data.factory;
1955 }
1956}
1957
1958
Benny Prijono6ba8c542007-10-16 01:34:14 +00001959/* Callback upon NAT detection completion */
1960static void nat_detect_cb(void *user_data,
1961 const pj_stun_nat_detect_result *res)
1962{
1963 PJ_UNUSED_ARG(user_data);
1964
1965 pjsua_var.nat_in_progress = PJ_FALSE;
1966 pjsua_var.nat_status = res->status;
1967 pjsua_var.nat_type = res->nat_type;
1968
1969 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
1970 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
1971 }
1972}
1973
1974
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001975/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001976 * Detect NAT type.
1977 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00001978PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001979{
1980 pj_status_t status;
1981
Benny Prijono6ba8c542007-10-16 01:34:14 +00001982 if (pjsua_var.nat_in_progress)
1983 return PJ_SUCCESS;
1984
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001985 /* Make sure STUN server resolution has completed */
1986 status = pjsua_resolve_stun_server(PJ_TRUE);
1987 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001988 pjsua_var.nat_status = status;
1989 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001990 return status;
1991 }
1992
1993 /* Make sure we have STUN */
1994 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00001995 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
1996 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001997 }
1998
Benny Prijono6ba8c542007-10-16 01:34:14 +00001999 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
2000 &pjsua_var.stun_cfg,
2001 NULL, &nat_detect_cb);
2002
2003 if (status != PJ_SUCCESS) {
2004 pjsua_var.nat_status = status;
2005 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
2006 return status;
2007 }
2008
2009 pjsua_var.nat_in_progress = PJ_TRUE;
2010
2011 return PJ_SUCCESS;
2012}
2013
2014
2015/*
2016 * Get NAT type.
2017 */
2018PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
2019{
2020 *type = pjsua_var.nat_type;
2021 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002022}
2023
2024
2025/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002026 * Verify that valid SIP url is given.
2027 */
2028PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
2029{
2030 pjsip_uri *p;
2031 pj_pool_t *pool;
2032 char *url;
2033 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2034
2035 if (!len) return -1;
2036
2037 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
2038 if (!pool) return -1;
2039
Benny Prijonoa1e69682007-05-11 15:14:34 +00002040 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002041 pj_ansi_strcpy(url, c_url);
2042
2043 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002044 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
2045 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
2046 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002047 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002048 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002049
2050 pj_pool_release(pool);
2051 return p ? 0 : -1;
2052}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002053
2054
2055/*
2056 * This is a utility function to dump the stack states to log, using
2057 * verbosity level 3.
2058 */
2059PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2060{
2061 unsigned old_decor;
2062 unsigned i;
2063 char buf[1024];
2064
2065 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2066
2067 old_decor = pj_log_get_decor();
2068 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2069
2070 if (detail)
2071 pj_dump_config();
2072
2073 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2074
2075 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2076
2077 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2078 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2079 pjsua_call *call = &pjsua_var.calls[i];
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002080 pjmedia_transport_info tpinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002081 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002082
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002083 /* MSVC complains about tpinfo not being initialized */
Benny Prijono734fc2d2008-03-17 16:05:35 +00002084 //pj_bzero(&tpinfo, sizeof(tpinfo));
Benny Prijono4f093d22007-04-09 08:54:32 +00002085
Benny Prijono734fc2d2008-03-17 16:05:35 +00002086 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002087 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002088
Benny Prijono5186eae2007-12-03 14:38:25 +00002089 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002090 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002091 pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, addr_buf,
Benny Prijono5186eae2007-12-03 14:38:25 +00002092 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002093 }
2094
2095 pjsip_tsx_layer_dump(detail);
2096 pjsip_ua_dump(detail);
2097
2098
2099 /* Dump all invite sessions: */
2100 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2101
2102 if (pjsua_call_get_count() == 0) {
2103
2104 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2105
2106 } else {
2107 unsigned i;
2108
2109 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2110 if (pjsua_call_is_active(i)) {
2111 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
2112 PJ_LOG(3,(THIS_FILE, "%s", buf));
2113 }
2114 }
2115 }
2116
2117 /* Dump presence status */
2118 pjsua_pres_dump(detail);
2119
2120 pj_log_set_decor(old_decor);
2121 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2122}
2123