blob: 84bfe200154e497d0e8383deb1d91ebc5e86b79c [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono268ca612006-02-07 12:34:11 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Benny Prijono4f9f64e2006-02-27 00:00:30 +000020#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000021#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono268ca612006-02-07 12:34:11 +000023
Benny Prijono84126ab2006-02-09 09:30:09 +000024#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000025
26
Benny Prijonoeebe9af2006-06-13 22:57:13 +000027/* PJSUA application instance. */
28struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000029
30
Benny Prijono44e88ea2007-10-30 15:42:35 +000031PJ_DEF(struct pjsua_data*) pjsua_get_var(void)
32{
33 return &pjsua_var;
34}
35
36
Benny Prijonoeebe9af2006-06-13 22:57:13 +000037/* Display error */
38PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
39 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000040{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000042
Benny Prijonoeebe9af2006-06-13 22:57:13 +000043 pj_strerror(status, errmsg, sizeof(errmsg));
Benny Prijonod6e362a2008-07-19 17:53:47 +000044 PJ_LOG(1,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000045}
46
47
Benny Prijonoeebe9af2006-06-13 22:57:13 +000048static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000049{
50 unsigned i;
51
Benny Prijonoc97608e2007-03-23 16:34:20 +000052 pj_bzero(&pjsua_var, sizeof(pjsua_var));
53
Benny Prijonoeebe9af2006-06-13 22:57:13 +000054 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i)
55 pjsua_var.acc[i].index = i;
56
57 for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i)
58 pjsua_var.tpdata[i].index = i;
Benny Prijonoc97608e2007-03-23 16:34:20 +000059
60 pjsua_var.stun_status = PJ_EUNKNOWN;
Benny Prijono6ba8c542007-10-16 01:34:14 +000061 pjsua_var.nat_status = PJ_EPENDING;
Benny Prijonoeebe9af2006-06-13 22:57:13 +000062}
Benny Prijonodc39fe82006-05-26 12:17:46 +000063
64
Benny Prijono1f61a8f2007-08-16 10:11:44 +000065PJ_DEF(void) pjsua_logging_config_default(pjsua_logging_config *cfg)
66{
67 pj_bzero(cfg, sizeof(*cfg));
68
69 cfg->msg_logging = PJ_TRUE;
70 cfg->level = 5;
71 cfg->console_level = 4;
72 cfg->decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
Benny Prijonod6e362a2008-07-19 17:53:47 +000073 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE |
74 PJ_LOG_HAS_SPACE;
75#if defined(PJ_WIN32) && PJ_WIN32 != 0
76 cfg->decor |= PJ_LOG_HAS_COLOR;
77#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +000078}
79
80PJ_DEF(void) pjsua_logging_config_dup(pj_pool_t *pool,
81 pjsua_logging_config *dst,
82 const pjsua_logging_config *src)
83{
84 pj_memcpy(dst, src, sizeof(*src));
85 pj_strdup_with_null(pool, &dst->log_filename, &src->log_filename);
86}
87
88PJ_DEF(void) pjsua_config_default(pjsua_config *cfg)
89{
90 pj_bzero(cfg, sizeof(*cfg));
91
92 cfg->max_calls = 4;
93 cfg->thread_cnt = 1;
Benny Prijono91a6a172007-10-31 08:59:29 +000094 cfg->nat_type_in_sdp = 1;
Benny Prijono91d06b62008-09-20 12:16:56 +000095 cfg->force_lr = PJ_TRUE;
Benny Prijonod8179652008-01-23 20:39:07 +000096#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
97 cfg->use_srtp = PJSUA_DEFAULT_USE_SRTP;
98 cfg->srtp_secure_signaling = PJSUA_DEFAULT_SRTP_SECURE_SIGNALING;
99#endif
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000100 cfg->hangup_forked_call = PJ_TRUE;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000101}
102
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000103PJ_DEF(void) pjsua_config_dup(pj_pool_t *pool,
104 pjsua_config *dst,
105 const pjsua_config *src)
106{
107 unsigned i;
108
109 pj_memcpy(dst, src, sizeof(*src));
110
111 for (i=0; i<src->outbound_proxy_cnt; ++i) {
112 pj_strdup_with_null(pool, &dst->outbound_proxy[i],
113 &src->outbound_proxy[i]);
114 }
115
116 for (i=0; i<src->cred_count; ++i) {
117 pjsip_cred_dup(pool, &dst->cred_info[i], &src->cred_info[i]);
118 }
119
120 pj_strdup_with_null(pool, &dst->user_agent, &src->user_agent);
121 pj_strdup_with_null(pool, &dst->stun_domain, &src->stun_domain);
122 pj_strdup_with_null(pool, &dst->stun_host, &src->stun_host);
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000123}
124
125PJ_DEF(void) pjsua_msg_data_init(pjsua_msg_data *msg_data)
126{
127 pj_bzero(msg_data, sizeof(*msg_data));
128 pj_list_init(&msg_data->hdr_list);
129}
130
131PJ_DEF(void) pjsua_transport_config_default(pjsua_transport_config *cfg)
132{
133 pj_bzero(cfg, sizeof(*cfg));
134 pjsip_tls_setting_default(&cfg->tls_setting);
135}
136
137PJ_DEF(void) pjsua_transport_config_dup(pj_pool_t *pool,
138 pjsua_transport_config *dst,
139 const pjsua_transport_config *src)
140{
141 PJ_UNUSED_ARG(pool);
142 pj_memcpy(dst, src, sizeof(*src));
143}
144
145PJ_DEF(void) pjsua_acc_config_default(pjsua_acc_config *cfg)
146{
147 pj_bzero(cfg, sizeof(*cfg));
148
149 cfg->reg_timeout = PJSUA_REG_INTERVAL;
150 cfg->transport_id = PJSUA_INVALID_ID;
Benny Prijonoe8554ef2008-03-22 09:33:52 +0000151 cfg->allow_contact_rewrite = PJ_TRUE;
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000152 cfg->require_100rel = pjsua_var.ua_cfg.require_100rel;
Benny Prijonobddef2c2007-10-31 13:28:08 +0000153 cfg->ka_interval = 15;
154 cfg->ka_data = pj_str("\r\n");
Benny Prijonod8179652008-01-23 20:39:07 +0000155#if defined(PJMEDIA_HAS_SRTP) && (PJMEDIA_HAS_SRTP != 0)
156 cfg->use_srtp = pjsua_var.ua_cfg.use_srtp;
157 cfg->srtp_secure_signaling = pjsua_var.ua_cfg.srtp_secure_signaling;
158#endif
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000159}
160
161PJ_DEF(void) pjsua_buddy_config_default(pjsua_buddy_config *cfg)
162{
163 pj_bzero(cfg, sizeof(*cfg));
164}
165
166PJ_DEF(void) pjsua_media_config_default(pjsua_media_config *cfg)
167{
168 pj_bzero(cfg, sizeof(*cfg));
169
170 cfg->clock_rate = PJSUA_DEFAULT_CLOCK_RATE;
Benny Prijono50f19b32008-03-11 13:15:43 +0000171 cfg->snd_clock_rate = 0;
Benny Prijono7d60d052008-03-29 12:24:20 +0000172 cfg->channel_count = 1;
Benny Prijono37c710b2008-01-10 12:09:26 +0000173 cfg->audio_frame_ptime = PJSUA_DEFAULT_AUDIO_FRAME_PTIME;
174 cfg->max_media_ports = PJSUA_MAX_CONF_PORTS;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000175 cfg->has_ioqueue = PJ_TRUE;
176 cfg->thread_cnt = 1;
177 cfg->quality = PJSUA_DEFAULT_CODEC_QUALITY;
178 cfg->ilbc_mode = PJSUA_DEFAULT_ILBC_MODE;
179 cfg->ec_tail_len = PJSUA_DEFAULT_EC_TAIL_LEN;
Benny Prijono10454dc2009-02-21 14:21:59 +0000180 cfg->snd_rec_latency = PJMEDIA_SND_DEFAULT_REC_LATENCY;
181 cfg->snd_play_latency = PJMEDIA_SND_DEFAULT_PLAY_LATENCY;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000182 cfg->jb_init = cfg->jb_min_pre = cfg->jb_max_pre = cfg->jb_max = -1;
Benny Prijonof798e502009-03-09 13:08:16 +0000183 cfg->snd_auto_close_time = 1;
Benny Prijonof76e1392008-06-06 14:51:48 +0000184
185 cfg->turn_conn_type = PJ_TURN_TP_UDP;
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000186}
187
Benny Prijonodc39fe82006-05-26 12:17:46 +0000188
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000189/*****************************************************************************
190 * This is a very simple PJSIP module, whose sole purpose is to display
191 * incoming and outgoing messages to log. This module will have priority
192 * higher than transport layer, which means:
193 *
194 * - incoming messages will come to this module first before reaching
195 * transaction layer.
196 *
197 * - outgoing messages will come to this module last, after the message
198 * has been 'printed' to contiguous buffer by transport layer and
199 * appropriate transport instance has been decided for this message.
200 *
201 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000202
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000203/* Notification on incoming messages */
204static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
205{
Benny Prijonob4a17c92006-07-10 14:40:21 +0000206 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
207 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000208 "--end msg--",
209 rdata->msg_info.len,
210 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000211 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000212 rdata->pkt_info.src_name,
213 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000214 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000215 rdata->msg_info.msg_buf));
216
217 /* Always return false, otherwise messages will not get processed! */
218 return PJ_FALSE;
219}
Benny Prijonodc39fe82006-05-26 12:17:46 +0000220
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000221/* Notification on outgoing messages */
222static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
223{
224
225 /* Important note:
226 * tp_info field is only valid after outgoing messages has passed
227 * transport layer. So don't try to access tp_info when the module
228 * has lower priority than transport layer.
229 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000230
Benny Prijonob4a17c92006-07-10 14:40:21 +0000231 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
232 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000233 "--end msg--",
234 (tdata->buf.cur - tdata->buf.start),
235 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000236 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000237 tdata->tp_info.dst_name,
238 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000239 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000240 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000241
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000242 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000243 return PJ_SUCCESS;
244}
245
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000246/* The module instance. */
247static pjsip_module pjsua_msg_logger =
248{
249 NULL, NULL, /* prev, next. */
250 { "mod-pjsua-log", 13 }, /* Name. */
251 -1, /* Id */
252 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
253 NULL, /* load() */
254 NULL, /* start() */
255 NULL, /* stop() */
256 NULL, /* unload() */
257 &logging_on_rx_msg, /* on_rx_request() */
258 &logging_on_rx_msg, /* on_rx_response() */
259 &logging_on_tx_msg, /* on_tx_request. */
260 &logging_on_tx_msg, /* on_tx_response() */
261 NULL, /* on_tsx_state() */
262
263};
264
265
266/*****************************************************************************
Benny Prijono56315612006-07-18 14:39:40 +0000267 * Another simple module to handle incoming OPTIONS request
268 */
269
270/* Notification on incoming request */
271static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata)
272{
273 pjsip_tx_data *tdata;
274 pjsip_response_addr res_addr;
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000275 pjmedia_transport_info tpinfo;
Benny Prijono56315612006-07-18 14:39:40 +0000276 pjmedia_sdp_session *sdp;
277 const pjsip_hdr *cap_hdr;
278 pj_status_t status;
279
280 /* Only want to handle OPTIONS requests */
281 if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method,
Benny Prijono1f61a8f2007-08-16 10:11:44 +0000282 pjsip_get_options_method()) != 0)
Benny Prijono56315612006-07-18 14:39:40 +0000283 {
284 return PJ_FALSE;
285 }
286
287 /* Create basic response. */
288 status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL,
289 &tdata);
290 if (status != PJ_SUCCESS) {
291 pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status);
292 return PJ_TRUE;
293 }
294
295 /* Add Allow header */
296 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL);
297 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000298 pjsip_msg_add_hdr(tdata->msg,
299 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000300 }
301
302 /* Add Accept header */
303 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL);
304 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000305 pjsip_msg_add_hdr(tdata->msg,
306 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000307 }
308
309 /* Add Supported header */
310 cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL);
311 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000312 pjsip_msg_add_hdr(tdata->msg,
313 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000314 }
315
316 /* Add Allow-Events header from the evsub module */
317 cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL);
318 if (cap_hdr) {
Benny Prijonoa1e69682007-05-11 15:14:34 +0000319 pjsip_msg_add_hdr(tdata->msg,
320 (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, cap_hdr));
Benny Prijono56315612006-07-18 14:39:40 +0000321 }
322
323 /* Add User-Agent header */
324 if (pjsua_var.ua_cfg.user_agent.slen) {
325 const pj_str_t USER_AGENT = { "User-Agent", 10};
326 pjsip_hdr *h;
327
328 h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool,
329 &USER_AGENT,
330 &pjsua_var.ua_cfg.user_agent);
331 pjsip_msg_add_hdr(tdata->msg, h);
332 }
333
Benny Prijono617c5bc2007-04-02 19:51:21 +0000334 /* Get media socket info */
Benny Prijono734fc2d2008-03-17 16:05:35 +0000335 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000336 pjmedia_transport_get_info(pjsua_var.calls[0].med_tp, &tpinfo);
Benny Prijono617c5bc2007-04-02 19:51:21 +0000337
Benny Prijono56315612006-07-18 14:39:40 +0000338 /* Add SDP body, using call0's RTP address */
339 status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1,
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000340 &tpinfo.sock_info, &sdp);
Benny Prijono56315612006-07-18 14:39:40 +0000341 if (status == PJ_SUCCESS) {
342 pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body);
343 }
344
345 /* Send response statelessly */
346 pjsip_get_response_addr(tdata->pool, rdata, &res_addr);
347 status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL);
348 if (status != PJ_SUCCESS)
349 pjsip_tx_data_dec_ref(tdata);
350
351 return PJ_TRUE;
352}
353
354
355/* The module instance. */
356static pjsip_module pjsua_options_handler =
357{
358 NULL, NULL, /* prev, next. */
359 { "mod-pjsua-options", 17 }, /* Name. */
360 -1, /* Id */
361 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
362 NULL, /* load() */
363 NULL, /* start() */
364 NULL, /* stop() */
365 NULL, /* unload() */
366 &options_on_rx_request, /* on_rx_request() */
367 NULL, /* on_rx_response() */
368 NULL, /* on_tx_request. */
369 NULL, /* on_tx_response() */
370 NULL, /* on_tsx_state() */
371
372};
373
374
375/*****************************************************************************
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000376 * These two functions are the main callbacks registered to PJSIP stack
377 * to receive SIP request and response messages that are outside any
378 * dialogs and any transactions.
379 */
Benny Prijono268ca612006-02-07 12:34:11 +0000380
381/*
382 * Handler for receiving incoming requests.
383 *
384 * This handler serves multiple purposes:
385 * - it receives requests outside dialogs.
386 * - it receives requests inside dialogs, when the requests are
387 * unhandled by other dialog usages. Example of these
388 * requests are: MESSAGE.
389 */
390static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
391{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000392 pj_bool_t processed = PJ_FALSE;
393
394 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000395
Benny Prijono84126ab2006-02-09 09:30:09 +0000396 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000397
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000398 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000399 }
400
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000401 PJSUA_UNLOCK();
402
403 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000404}
405
406
407/*
408 * Handler for receiving incoming responses.
409 *
410 * This handler serves multiple purposes:
411 * - it receives strayed responses (i.e. outside any dialog and
412 * outside any transactions).
413 * - it receives responses coming to a transaction, when pjsua
414 * module is set as transaction user for the transaction.
415 * - it receives responses inside a dialog, when these responses
416 * are unhandled by other dialog usages.
417 */
418static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
419{
420 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000421 return PJ_FALSE;
422}
423
424
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425/*****************************************************************************
426 * Logging.
427 */
428
429/* Log callback */
430static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000431{
Benny Prijono572d4852006-11-23 21:50:02 +0000432 /* Write to file, stdout or application callback. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000433
434 if (pjsua_var.log_file) {
435 pj_ssize_t size = len;
436 pj_file_write(pjsua_var.log_file, buffer, &size);
Benny Prijono980ca0a2007-04-03 17:55:08 +0000437 /* This will slow things down considerably! Don't do it!
438 pj_file_flush(pjsua_var.log_file);
439 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440 }
441
Benny Prijono572d4852006-11-23 21:50:02 +0000442 if (level <= (int)pjsua_var.log_cfg.console_level) {
443 if (pjsua_var.log_cfg.cb)
444 (*pjsua_var.log_cfg.cb)(level, buffer, len);
445 else
446 pj_log_write(level, buffer, len);
447 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000448}
449
450
451/*
452 * Application can call this function at any time (after pjsua_create(), of
453 * course) to change logging settings.
454 */
455PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg)
456{
457 pj_status_t status;
458
459 /* Save config. */
460 pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg);
461
462 /* Redirect log function to ours */
463 pj_log_set_log_func( &log_writer );
464
Benny Prijono9cb09a22007-08-30 09:35:10 +0000465 /* Set decor */
466 pj_log_set_decor(pjsua_var.log_cfg.decor);
467
Benny Prijono4190cf92008-01-18 13:25:05 +0000468 /* Set log level */
469 pj_log_set_level(pjsua_var.log_cfg.level);
470
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000471 /* Close existing file, if any */
472 if (pjsua_var.log_file) {
473 pj_file_close(pjsua_var.log_file);
474 pjsua_var.log_file = NULL;
475 }
476
477 /* If output log file is desired, create the file: */
478 if (pjsua_var.log_cfg.log_filename.slen) {
479
480 status = pj_file_open(pjsua_var.pool,
481 pjsua_var.log_cfg.log_filename.ptr,
482 PJ_O_WRONLY,
483 &pjsua_var.log_file);
484
485 if (status != PJ_SUCCESS) {
486 pjsua_perror(THIS_FILE, "Error creating log file", status);
487 return status;
488 }
489 }
490
491 /* Unregister msg logging if it's previously registered */
492 if (pjsua_msg_logger.id >= 0) {
493 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
494 pjsua_msg_logger.id = -1;
495 }
496
497 /* Enable SIP message logging */
498 if (pjsua_var.log_cfg.msg_logging)
499 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
500
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000501 return PJ_SUCCESS;
502}
503
504
505/*****************************************************************************
506 * PJSUA Base API.
507 */
508
509/* Worker thread function. */
510static int worker_thread(void *arg)
511{
512 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000513
Benny Prijono268ca612006-02-07 12:34:11 +0000514 PJ_UNUSED_ARG(arg);
515
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000516 while (!pjsua_var.thread_quit_flag) {
517 int count;
518
519 count = pjsua_handle_events(TIMEOUT);
520 if (count < 0)
521 pj_thread_sleep(TIMEOUT);
522 }
Benny Prijono268ca612006-02-07 12:34:11 +0000523
524 return 0;
525}
526
Benny Prijonodc39fe82006-05-26 12:17:46 +0000527
Benny Prijono8389c312008-02-21 21:36:34 +0000528/* Init random seed */
529static void init_random_seed(void)
530{
531 pj_sockaddr addr;
532 const pj_str_t *hostname;
533 pj_uint32_t pid;
534 pj_time_val t;
535 unsigned seed=0;
536
537 /* Add hostname */
538 hostname = pj_gethostname();
539 seed = pj_hash_calc(seed, hostname->ptr, (int)hostname->slen);
540
541 /* Add primary IP address */
542 if (pj_gethostip(pj_AF_INET(), &addr)==PJ_SUCCESS)
543 seed = pj_hash_calc(seed, &addr.ipv4.sin_addr, 4);
544
545 /* Get timeofday */
546 pj_gettimeofday(&t);
547 seed = pj_hash_calc(seed, &t, sizeof(t));
548
549 /* Add PID */
550 pid = pj_getpid();
551 seed = pj_hash_calc(seed, &pid, sizeof(pid));
552
553 /* Init random seed */
554 pj_srand(seed);
555}
556
Benny Prijono268ca612006-02-07 12:34:11 +0000557/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000558 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000559 */
560PJ_DEF(pj_status_t) pjsua_create(void)
561{
562 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000563
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000564 /* Init pjsua data */
565 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000566
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000567 /* Set default logging settings */
568 pjsua_logging_config_default(&pjsua_var.log_cfg);
569
570 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000571 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000572 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
573
Benny Prijono8389c312008-02-21 21:36:34 +0000574 /* Init random seed */
575 init_random_seed();
Benny Prijono268ca612006-02-07 12:34:11 +0000576
Benny Prijonofccab712006-02-22 22:23:22 +0000577 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000578 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000579 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000580
Benny Prijonoec921342007-03-24 13:00:30 +0000581 /* Init PJNATH */
582 status = pjnath_init();
583 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono268ca612006-02-07 12:34:11 +0000584
Benny Prijono094d3ad2006-11-21 08:41:00 +0000585 /* Set default sound device ID */
Benny Prijono96e74f32009-02-22 12:00:12 +0000586 pjsua_var.cap_dev = PJMEDIA_AUD_DEFAULT_CAPTURE_DEV;
587 pjsua_var.play_dev = PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV;
Benny Prijono094d3ad2006-11-21 08:41:00 +0000588
Benny Prijono268ca612006-02-07 12:34:11 +0000589 /* Init caching pool. */
Benny Prijono106f5b72007-08-30 13:49:33 +0000590 pj_caching_pool_init(&pjsua_var.cp, NULL, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000591
592 /* Create memory pool for application. */
Benny Prijonoc91ed8d2008-07-13 12:24:55 +0000593 pjsua_var.pool = pjsua_pool_create("pjsua", 1000, 1000);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594
595 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000596
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000597 /* Create mutex */
598 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
599 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000600 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000601 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000602 return status;
603 }
604
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000605 /* Must create SIP endpoint to initialize SIP parser. The parser
606 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000607 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000608 status = pjsip_endpt_create(&pjsua_var.cp.factory,
609 pj_gethostname()->ptr,
610 &pjsua_var.endpt);
611 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000612
613
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000614 return PJ_SUCCESS;
615}
616
617
618/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000619 * Initialize pjsua with the specified settings. All the settings are
620 * optional, and the default values will be used when the config is not
621 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000622 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000623PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
624 const pjsua_logging_config *log_cfg,
625 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000626{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000627 pjsua_config default_cfg;
628 pjsua_media_config default_media_cfg;
Benny Prijonoc8141a82006-08-20 09:12:19 +0000629 const pj_str_t STR_OPTIONS = { "OPTIONS", 7 };
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000630 pjsip_ua_init_param ua_init_param;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000631 pj_status_t status;
632
633
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000634 /* Create default configurations when the config is not supplied */
635
636 if (ua_cfg == NULL) {
637 pjsua_config_default(&default_cfg);
638 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000639 }
640
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000641 if (media_cfg == NULL) {
642 pjsua_media_config_default(&default_media_cfg);
643 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000644 }
645
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000646 /* Initialize logging first so that info/errors can be captured */
647 if (log_cfg) {
648 status = pjsua_reconfigure_logging(log_cfg);
649 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000650 }
651
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000652 /* If nameserver is configured, create DNS resolver instance and
653 * set it to be used by SIP resolver.
654 */
655 if (ua_cfg->nameserver_count) {
656#if PJSIP_HAS_RESOLVER
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000657 unsigned i;
658
659 /* Create DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000660 status = pjsip_endpt_create_resolver(pjsua_var.endpt,
661 &pjsua_var.resolver);
Benny Prijono318f3842007-05-15 20:27:08 +0000662 if (status != PJ_SUCCESS) {
663 pjsua_perror(THIS_FILE, "Error creating resolver", status);
664 return status;
665 }
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000666
667 /* Configure nameserver for the DNS resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000668 status = pj_dns_resolver_set_ns(pjsua_var.resolver,
669 ua_cfg->nameserver_count,
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000670 ua_cfg->nameserver, NULL);
671 if (status != PJ_SUCCESS) {
672 pjsua_perror(THIS_FILE, "Error setting nameserver", status);
673 return status;
674 }
675
676 /* Set this DNS resolver to be used by the SIP resolver */
Benny Prijonoc97608e2007-03-23 16:34:20 +0000677 status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver);
Benny Prijonofa9e5b12006-10-08 12:39:34 +0000678 if (status != PJ_SUCCESS) {
679 pjsua_perror(THIS_FILE, "Error setting DNS resolver", status);
680 return status;
681 }
682
683 /* Print nameservers */
684 for (i=0; i<ua_cfg->nameserver_count; ++i) {
685 PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added",
686 (int)ua_cfg->nameserver[i].slen,
687 ua_cfg->nameserver[i].ptr));
688 }
689#else
690 PJ_LOG(2,(THIS_FILE,
691 "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)"));
692#endif
693 }
694
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000695 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000696
697 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000698 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
699 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000700
Benny Prijonodc39fe82006-05-26 12:17:46 +0000701
702 /* Initialize UA layer module: */
Benny Prijono3c5e28b2008-09-24 10:10:15 +0000703 pj_bzero(&ua_init_param, sizeof(ua_init_param));
704 if (ua_cfg->hangup_forked_call) {
705 ua_init_param.on_dlg_forked = &on_dlg_forked;
706 }
707 status = pjsip_ua_init_module( pjsua_var.endpt, &ua_init_param);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000708 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000709
Benny Prijonodc39fe82006-05-26 12:17:46 +0000710
Benny Prijono053f5222006-11-11 16:16:04 +0000711 /* Initialize Replaces support. */
712 status = pjsip_replaces_init_module( pjsua_var.endpt );
713 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
714
Benny Prijonodcfc0ba2007-09-30 16:50:27 +0000715 /* Initialize 100rel support */
716 status = pjsip_100rel_init_module(pjsua_var.endpt);
717 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono053f5222006-11-11 16:16:04 +0000718
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000719 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000720 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000721 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000722 {
723 NULL, NULL, /* prev, next. */
724 { "mod-pjsua", 9 }, /* Name. */
725 -1, /* Id */
726 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
727 NULL, /* load() */
728 NULL, /* start() */
729 NULL, /* stop() */
730 NULL, /* unload() */
731 &mod_pjsua_on_rx_request, /* on_rx_request() */
732 &mod_pjsua_on_rx_response, /* on_rx_response() */
733 NULL, /* on_tx_request. */
734 NULL, /* on_tx_response() */
735 NULL, /* on_tsx_state() */
736 };
737
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000738 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000739
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000740 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
741 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000742 }
743
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000744
Benny Prijonodc39fe82006-05-26 12:17:46 +0000745
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000746 /* Initialize PJSUA call subsystem: */
747 status = pjsua_call_subsys_init(ua_cfg);
748 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000749 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000750
751
Benny Prijonoc97608e2007-03-23 16:34:20 +0000752 /* Start resolving STUN server */
Benny Prijono4ab9fbb2007-10-12 12:14:27 +0000753
Benny Prijonoc97608e2007-03-23 16:34:20 +0000754 status = pjsua_resolve_stun_server(PJ_FALSE);
755 if (status != PJ_SUCCESS && status != PJ_EPENDING) {
756 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
757 return status;
758 }
759
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000760 /* Initialize PJSUA media subsystem */
761 status = pjsua_media_subsys_init(media_cfg);
762 if (status != PJ_SUCCESS)
763 goto on_error;
764
Benny Prijonodc39fe82006-05-26 12:17:46 +0000765
766 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000767 status = pjsip_evsub_init_module(pjsua_var.endpt);
768 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000769
Benny Prijonodc39fe82006-05-26 12:17:46 +0000770
771 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000772 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
773 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000774
Benny Prijono3a5e1ab2006-08-15 20:26:34 +0000775 /* Init PUBLISH module */
776 pjsip_publishc_init_module(pjsua_var.endpt);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000777
778 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000779 status = pjsip_xfer_init_module( pjsua_var.endpt );
780 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000781
782 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000783 status = pjsua_pres_init();
784 if (status != PJ_SUCCESS)
785 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000786
787 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000788 status = pjsua_im_init();
789 if (status != PJ_SUCCESS)
790 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000791
Benny Prijonoc8141a82006-08-20 09:12:19 +0000792 /* Register OPTIONS handler */
793 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
794
795 /* Add OPTIONS in Allow header */
796 pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW,
797 NULL, 1, &STR_OPTIONS);
798
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000799 /* Start worker thread if needed. */
800 if (pjsua_var.ua_cfg.thread_cnt) {
801 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000802
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000803 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
804 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000805
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000806 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
807 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
808 NULL, 0, 0, &pjsua_var.thread[i]);
809 if (status != PJ_SUCCESS)
810 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000811 }
Benny Prijonof521eb02006-08-06 23:07:25 +0000812 PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created",
813 pjsua_var.ua_cfg.thread_cnt));
814 } else {
815 PJ_LOG(4,(THIS_FILE, "No SIP worker threads created"));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000816 }
817
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000818 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000819
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000820 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
Benny Prijono106f5b72007-08-30 13:49:33 +0000821 pj_get_version(), PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000822
Benny Prijono268ca612006-02-07 12:34:11 +0000823 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000824
825on_error:
826 pjsua_destroy();
827 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000828}
829
830
Benny Prijono834aee32006-02-19 01:38:06 +0000831/* Sleep with polling */
832static void busy_sleep(unsigned msec)
833{
834 pj_time_val timeout, now;
835
836 pj_gettimeofday(&timeout);
837 timeout.msec += msec;
838 pj_time_val_normalize(&timeout);
839
840 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000841 while (pjsua_handle_events(10) > 0)
842 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000843 pj_gettimeofday(&now);
844 } while (PJ_TIME_VAL_LT(now, timeout));
845}
846
Benny Prijonoebbf6892007-03-24 17:37:25 +0000847
Benny Prijonoebbf6892007-03-24 17:37:25 +0000848/*
849 * Callback function to receive notification from the resolver
850 * when the resolution process completes.
851 */
852static void stun_dns_srv_resolver_cb(void *user_data,
853 pj_status_t status,
854 const pj_dns_srv_record *rec)
855{
856 unsigned i;
857
858 PJ_UNUSED_ARG(user_data);
859
860 pjsua_var.stun_status = status;
861
862 if (status != PJ_SUCCESS) {
863 /* DNS SRV resolution failed. If stun_host is specified, resolve
864 * it with gethostbyname()
865 */
866 if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000867 pj_str_t str_host, str_port;
868 int port;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000869 pj_hostent he;
870
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000871 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
872 if (str_port.ptr != NULL) {
873 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
874 str_host.slen = (str_port.ptr - str_host.ptr);
875 str_port.ptr++;
876 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
877 str_host.slen - 1;
878 port = (int)pj_strtoul(&str_port);
879 if (port < 1 || port > 65535) {
880 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
881 pjsua_var.stun_status = PJ_EINVAL;
882 return;
883 }
884 } else {
885 str_host = pjsua_var.ua_cfg.stun_host;
886 port = 3478;
887 }
888
889 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000890
891 if (pjsua_var.stun_status == PJ_SUCCESS) {
892 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
893 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000894 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
Benny Prijonoebbf6892007-03-24 17:37:25 +0000895
896 PJ_LOG(3,(THIS_FILE,
897 "STUN server %.*s resolved, address is %s:%d",
898 (int)pjsua_var.ua_cfg.stun_host.slen,
899 pjsua_var.ua_cfg.stun_host.ptr,
900 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
901 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
902 }
903 } else {
904 char errmsg[PJ_ERR_MSG_SIZE];
905
906 pj_strerror(status, errmsg, sizeof(errmsg));
907 PJ_LOG(1,(THIS_FILE,
908 "DNS SRV resolution failed for STUN server %.*s: %s",
909 (int)pjsua_var.ua_cfg.stun_domain.slen,
910 pjsua_var.ua_cfg.stun_domain.ptr,
911 errmsg));
912 }
913 return;
914 }
915
Benny Prijonof5afa922007-06-11 16:51:18 +0000916 pj_assert(rec->count != 0 && rec->entry[0].server.addr_count != 0);
917 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL,
918 rec->entry[0].port);
919 pjsua_var.stun_srv.ipv4.sin_addr.s_addr =
920 rec->entry[0].server.addr[0].s_addr;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000921
922 PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):",
923 (int)pjsua_var.ua_cfg.stun_domain.slen,
924 pjsua_var.ua_cfg.stun_domain.ptr,
925 rec->count));
926
927 for (i=0; i<rec->count; ++i) {
928 PJ_LOG(3,(THIS_FILE,
929 " %d: prio=%d, weight=%d %s:%d",
930 i, rec->entry[i].priority, rec->entry[i].weight,
Benny Prijonof5afa922007-06-11 16:51:18 +0000931 pj_inet_ntoa(rec->entry[i].server.addr[0]),
932 (int)rec->entry[i].port));
Benny Prijonoebbf6892007-03-24 17:37:25 +0000933 }
934
935}
936
Benny Prijono268ca612006-02-07 12:34:11 +0000937/*
Benny Prijonoc97608e2007-03-23 16:34:20 +0000938 * Resolve STUN server.
939 */
940pj_status_t pjsua_resolve_stun_server(pj_bool_t wait)
941{
942 if (pjsua_var.stun_status == PJ_EUNKNOWN) {
943 /* Initialize STUN configuration */
944 pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0,
945 pjsip_endpt_get_ioqueue(pjsua_var.endpt),
946 pjsip_endpt_get_timer_heap(pjsua_var.endpt));
947
948 /* Start STUN server resolution */
Benny Prijonoebbf6892007-03-24 17:37:25 +0000949
950 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000951
Benny Prijonoebbf6892007-03-24 17:37:25 +0000952 /* If stun_domain is specified, resolve STUN servers with DNS
953 * SRV resolution.
954 */
955 if (pjsua_var.ua_cfg.stun_domain.slen) {
956 pj_str_t res_type;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000957 pj_status_t status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000958
959 /* Fail if resolver is not configured */
960 if (pjsua_var.resolver == NULL) {
961 PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when "
962 "stun_domain is specified"));
963 pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS;
964 return PJLIB_UTIL_EDNSNONS;
965 }
966 res_type = pj_str("_stun._udp");
Benny Prijonoda9785b2007-04-02 20:43:06 +0000967 status =
Benny Prijonoebbf6892007-03-24 17:37:25 +0000968 pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type,
969 3478, pjsua_var.pool, pjsua_var.resolver,
Benny Prijonof5afa922007-06-11 16:51:18 +0000970 0, NULL, &stun_dns_srv_resolver_cb, NULL);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000971 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +0000972 pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution",
973 pjsua_var.stun_status);
Benny Prijonoda9785b2007-04-02 20:43:06 +0000974 pjsua_var.stun_status = status;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000975 return pjsua_var.stun_status;
Benny Prijonoda9785b2007-04-02 20:43:06 +0000976 } else {
977 pjsua_var.stun_status = PJ_EPENDING;
Benny Prijonoebbf6892007-03-24 17:37:25 +0000978 }
979 }
980 /* Otherwise if stun_host is specified, resolve STUN server with
981 * gethostbyname().
982 */
983 else if (pjsua_var.ua_cfg.stun_host.slen) {
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000984 pj_str_t str_host, str_port;
985 int port;
Benny Prijonoc97608e2007-03-23 16:34:20 +0000986 pj_hostent he;
987
Benny Prijonoaf09dc32007-04-22 12:48:30 +0000988 str_port.ptr = pj_strchr(&pjsua_var.ua_cfg.stun_host, ':');
989 if (str_port.ptr != NULL) {
990 str_host.ptr = pjsua_var.ua_cfg.stun_host.ptr;
991 str_host.slen = (str_port.ptr - str_host.ptr);
992 str_port.ptr++;
993 str_port.slen = pjsua_var.ua_cfg.stun_host.slen -
994 str_host.slen - 1;
995 port = (int)pj_strtoul(&str_port);
996 if (port < 1 || port > 65535) {
997 pjsua_perror(THIS_FILE, "Invalid STUN server", PJ_EINVAL);
998 pjsua_var.stun_status = PJ_EINVAL;
999 return pjsua_var.stun_status;
1000 }
1001 } else {
1002 str_host = pjsua_var.ua_cfg.stun_host;
1003 port = 3478;
1004 }
1005
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001006 pjsua_var.stun_status =
1007 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, &str_host,
1008 (pj_uint16_t)port);
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001009
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001010 if (pjsua_var.stun_status != PJ_SUCCESS) {
1011 pjsua_var.stun_status = pj_gethostbyname(&str_host, &he);
Benny Prijonoc97608e2007-03-23 16:34:20 +00001012
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001013 if (pjsua_var.stun_status == PJ_SUCCESS) {
1014 pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0);
1015 pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr;
1016 pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)port);
1017 }
Benny Prijonoc97608e2007-03-23 16:34:20 +00001018 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001019
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00001020 PJ_LOG(3,(THIS_FILE,
1021 "STUN server %.*s resolved, address is %s:%d",
1022 (int)pjsua_var.ua_cfg.stun_host.slen,
1023 pjsua_var.ua_cfg.stun_host.ptr,
1024 pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr),
1025 (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port)));
1026
Benny Prijonoc97608e2007-03-23 16:34:20 +00001027 }
Benny Prijonoebbf6892007-03-24 17:37:25 +00001028 /* Otherwise disable STUN. */
1029 else {
1030 pjsua_var.stun_status = PJ_SUCCESS;
1031 }
1032
1033
Benny Prijonoc97608e2007-03-23 16:34:20 +00001034 return pjsua_var.stun_status;
1035
1036 } else if (pjsua_var.stun_status == PJ_EPENDING) {
1037 /* STUN server resolution has been started, wait for the
1038 * result.
1039 */
Benny Prijonoebbf6892007-03-24 17:37:25 +00001040 if (wait) {
1041 while (pjsua_var.stun_status == PJ_EPENDING)
1042 pjsua_handle_events(10);
1043 }
1044
1045 return pjsua_var.stun_status;
Benny Prijonoc97608e2007-03-23 16:34:20 +00001046
1047 } else {
1048 /* STUN server has been resolved, return the status */
1049 return pjsua_var.stun_status;
1050 }
1051}
1052
1053/*
Benny Prijono268ca612006-02-07 12:34:11 +00001054 * Destroy pjsua.
1055 */
Benny Prijonodc39fe82006-05-26 12:17:46 +00001056PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +00001057{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001058 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001059
1060 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001061 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +00001062
1063 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001064 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
1065 if (pjsua_var.thread[i]) {
1066 pj_thread_join(pjsua_var.thread[i]);
1067 pj_thread_destroy(pjsua_var.thread[i]);
1068 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001069 }
Benny Prijono268ca612006-02-07 12:34:11 +00001070 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001071
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001072 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001073 /* Terminate all calls. */
1074 pjsua_call_hangup_all();
1075
Benny Prijono7f6ee022008-07-31 08:32:46 +00001076 /* Set all accounts to offline */
1077 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1078 if (!pjsua_var.acc[i].valid)
1079 continue;
1080 pjsua_var.acc[i].online_status = PJ_FALSE;
1081 pj_bzero(&pjsua_var.acc[i].rpid, sizeof(pjrpid_element));
1082 }
1083
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001084 /* Terminate all presence subscriptions. */
1085 pjsua_pres_shutdown();
1086
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001087 /* Unregister all accounts */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001088 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1089 if (!pjsua_var.acc[i].valid)
1090 continue;
1091
1092 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +00001093 pjsua_acc_set_registration(i, PJ_FALSE);
1094 }
1095 }
Benny Prijono9fc735d2006-05-28 14:58:12 +00001096 }
Benny Prijono834aee32006-02-19 01:38:06 +00001097
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001098 /* Destroy media */
1099 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001100
Benny Prijono268ca612006-02-07 12:34:11 +00001101 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001102 if (pjsua_var.endpt) {
Benny Prijonoff3b1462008-07-14 09:32:14 +00001103 /* Wait for some time to allow unregistration and ICE/TURN
1104 * transports shutdown to complete:
1105 */
1106 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1107 busy_sleep(1000);
1108
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001109 PJ_LOG(4,(THIS_FILE, "Destroying..."));
1110
Benny Prijonod7e26582008-07-18 23:51:49 +00001111 /* Must destroy endpoint first before destroying pools in
1112 * buddies or accounts, since shutting down transaction layer
1113 * may emit events which trigger some buddy or account callbacks
1114 * to be called.
1115 */
1116 pjsip_endpt_destroy(pjsua_var.endpt);
1117 pjsua_var.endpt = NULL;
1118
Benny Prijono4d1cc7b2008-07-14 09:55:01 +00001119 /* Destroy pool in the buddy object */
1120 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.buddy); ++i) {
1121 if (pjsua_var.buddy[i].pool) {
1122 pj_pool_release(pjsua_var.buddy[i].pool);
1123 pjsua_var.buddy[i].pool = NULL;
1124 }
1125 }
1126
1127 /* Destroy accounts */
1128 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
1129 if (pjsua_var.acc[i].pool) {
1130 pj_pool_release(pjsua_var.acc[i].pool);
1131 pjsua_var.acc[i].pool = NULL;
1132 }
1133 }
Benny Prijono9fc735d2006-05-28 14:58:12 +00001134 }
Benny Prijono268ca612006-02-07 12:34:11 +00001135
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001136 /* Destroy mutex */
1137 if (pjsua_var.mutex) {
1138 pj_mutex_destroy(pjsua_var.mutex);
1139 pjsua_var.mutex = NULL;
1140 }
1141
1142 /* Destroy pool and pool factory. */
1143 if (pjsua_var.pool) {
1144 pj_pool_release(pjsua_var.pool);
1145 pjsua_var.pool = NULL;
1146 pj_caching_pool_destroy(&pjsua_var.cp);
Benny Prijonoad2e0ca2007-04-29 12:31:51 +00001147
1148 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
1149
1150 /* End logging */
1151 if (pjsua_var.log_file) {
1152 pj_file_close(pjsua_var.log_file);
1153 pjsua_var.log_file = NULL;
1154 }
1155
1156 /* Shutdown PJLIB */
1157 pj_shutdown();
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001158 }
Benny Prijono268ca612006-02-07 12:34:11 +00001159
Benny Prijonof762ee72006-12-01 11:14:37 +00001160 /* Clear pjsua_var */
1161 pj_bzero(&pjsua_var, sizeof(pjsua_var));
1162
Benny Prijono268ca612006-02-07 12:34:11 +00001163 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001164 return PJ_SUCCESS;
1165}
1166
1167
1168/**
1169 * Application is recommended to call this function after all initialization
1170 * is done, so that the library can do additional checking set up
1171 * additional
1172 *
1173 * @return PJ_SUCCESS on success, or the appropriate error code.
1174 */
1175PJ_DEF(pj_status_t) pjsua_start(void)
1176{
1177 pj_status_t status;
1178
1179 status = pjsua_call_subsys_start();
1180 if (status != PJ_SUCCESS)
1181 return status;
1182
1183 status = pjsua_media_subsys_start();
1184 if (status != PJ_SUCCESS)
1185 return status;
1186
1187 status = pjsua_pres_start();
1188 if (status != PJ_SUCCESS)
1189 return status;
Benny Prijono268ca612006-02-07 12:34:11 +00001190
1191 return PJ_SUCCESS;
1192}
1193
Benny Prijono9fc735d2006-05-28 14:58:12 +00001194
1195/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001196 * Poll pjsua for events, and if necessary block the caller thread for
1197 * the specified maximum interval (in miliseconds).
1198 */
1199PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
1200{
Benny Prijono897f9f82007-05-03 19:56:21 +00001201#if defined(PJ_SYMBIAN) && PJ_SYMBIAN != 0
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001202
1203 return pj_symbianos_poll(-1, msec_timeout);
1204
Benny Prijono897f9f82007-05-03 19:56:21 +00001205#else
1206
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001207 unsigned count = 0;
1208 pj_time_val tv;
1209 pj_status_t status;
1210
1211 tv.sec = 0;
1212 tv.msec = msec_timeout;
1213 pj_time_val_normalize(&tv);
1214
1215 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
1216
1217 if (status != PJ_SUCCESS)
1218 return -status;
1219
1220 return count;
Nanang Izzuddin82f7a412008-12-17 11:36:22 +00001221
Benny Prijono897f9f82007-05-03 19:56:21 +00001222#endif
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001223}
1224
1225
1226/*
1227 * Create memory pool.
1228 */
1229PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
1230 pj_size_t increment)
1231{
1232 /* Pool factory is thread safe, no need to lock */
1233 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
1234 NULL);
1235}
1236
1237
1238/*
1239 * Internal function to get SIP endpoint instance of pjsua, which is
1240 * needed for example to register module, create transports, etc.
1241 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001242 */
1243PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
1244{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001245 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001246}
1247
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001248/*
1249 * Internal function to get media endpoint instance.
1250 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +00001251 */
1252PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
1253{
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001254 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +00001255}
1256
Benny Prijono97b87172006-08-24 14:25:14 +00001257/*
1258 * Internal function to get PJSUA pool factory.
1259 */
1260PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void)
1261{
1262 return &pjsua_var.cp.factory;
1263}
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001264
1265/*****************************************************************************
1266 * PJSUA SIP Transport API.
1267 */
1268
1269/*
Benny Prijono23674a32007-12-01 08:59:25 +00001270 * Tools to get address string.
1271 */
1272static const char *addr_string(const pj_sockaddr_t *addr)
1273{
1274 static char str[128];
1275 str[0] = '\0';
1276 pj_inet_ntop(((const pj_sockaddr*)addr)->addr.sa_family,
1277 pj_sockaddr_get_addr(addr),
1278 str, sizeof(str));
1279 return str;
1280}
1281
1282/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001283 * Create and initialize SIP socket (and possibly resolve public
1284 * address via STUN, depending on config).
1285 */
Benny Prijono23674a32007-12-01 08:59:25 +00001286static pj_status_t create_sip_udp_sock(int af,
1287 const pj_str_t *bind_param,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001288 int port,
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001289 pj_sock_t *p_sock,
Benny Prijono23674a32007-12-01 08:59:25 +00001290 pj_sockaddr *p_pub_addr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001291{
Benny Prijono23674a32007-12-01 08:59:25 +00001292 char stun_ip_addr[PJ_INET6_ADDRSTRLEN];
Benny Prijonoc97608e2007-03-23 16:34:20 +00001293 pj_str_t stun_srv;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001294 pj_sock_t sock;
Benny Prijono23674a32007-12-01 08:59:25 +00001295 pj_sockaddr bind_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001296 pj_status_t status;
1297
Benny Prijonoc97608e2007-03-23 16:34:20 +00001298 /* Make sure STUN server resolution has completed */
1299 status = pjsua_resolve_stun_server(PJ_TRUE);
1300 if (status != PJ_SUCCESS) {
1301 pjsua_perror(THIS_FILE, "Error resolving STUN server", status);
1302 return status;
1303 }
1304
Benny Prijono23674a32007-12-01 08:59:25 +00001305 /* Initialize bound address */
1306 if (bind_param->slen) {
1307 status = pj_sockaddr_init(af, &bind_addr, bind_param,
1308 (pj_uint16_t)port);
1309 if (status != PJ_SUCCESS) {
1310 pjsua_perror(THIS_FILE,
1311 "Unable to resolve transport bound address",
1312 status);
1313 return status;
1314 }
1315 } else {
1316 pj_sockaddr_init(af, &bind_addr, NULL, (pj_uint16_t)port);
1317 }
1318
1319 status = pj_sock_socket(af, pj_SOCK_DGRAM(), 0, &sock);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001320 if (status != PJ_SUCCESS) {
1321 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +00001322 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001323 }
1324
Benny Prijono5f55a1f2007-12-05 05:08:29 +00001325 status = pj_sock_bind(sock, &bind_addr, pj_sockaddr_get_len(&bind_addr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001326 if (status != PJ_SUCCESS) {
1327 pjsua_perror(THIS_FILE, "bind() error", status);
1328 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001329 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001330 }
1331
Benny Prijonoe347cb02007-02-14 14:36:13 +00001332 /* If port is zero, get the bound port */
1333 if (port == 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001334 pj_sockaddr bound_addr;
Benny Prijonoe347cb02007-02-14 14:36:13 +00001335 int namelen = sizeof(bound_addr);
1336 status = pj_sock_getsockname(sock, &bound_addr, &namelen);
1337 if (status != PJ_SUCCESS) {
1338 pjsua_perror(THIS_FILE, "getsockname() error", status);
1339 pj_sock_close(sock);
1340 return status;
1341 }
1342
Benny Prijono23674a32007-12-01 08:59:25 +00001343 port = pj_sockaddr_get_port(&bound_addr);
Benny Prijonoe347cb02007-02-14 14:36:13 +00001344 }
1345
Benny Prijonoc97608e2007-03-23 16:34:20 +00001346 if (pjsua_var.stun_srv.addr.sa_family != 0) {
Benny Prijono23674a32007-12-01 08:59:25 +00001347 pj_ansi_strcpy(stun_ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr));
1348 stun_srv = pj_str(stun_ip_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001349 } else {
Benny Prijonoc97608e2007-03-23 16:34:20 +00001350 stun_srv.slen = 0;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001351 }
1352
1353 /* Get the published address, either by STUN or by resolving
1354 * the name of local host.
1355 */
Benny Prijono23674a32007-12-01 08:59:25 +00001356 if (pj_sockaddr_has_addr(p_pub_addr)) {
Benny Prijono744aca02007-11-11 03:06:07 +00001357 /*
1358 * Public address is already specified, no need to resolve the
1359 * address, only set the port.
1360 */
Benny Prijono23674a32007-12-01 08:59:25 +00001361 if (pj_sockaddr_get_port(p_pub_addr) == 0)
1362 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijono744aca02007-11-11 03:06:07 +00001363
1364 } else if (stun_srv.slen) {
Benny Prijono0a5cad82006-09-26 13:21:02 +00001365 /*
1366 * STUN is specified, resolve the address with STUN.
1367 */
Benny Prijono23674a32007-12-01 08:59:25 +00001368 if (af != pj_AF_INET()) {
1369 pjsua_perror(THIS_FILE, "Cannot use STUN", PJ_EAFNOTSUP);
1370 pj_sock_close(sock);
1371 return PJ_EAFNOTSUP;
1372 }
1373
Benny Prijono14c2b862007-02-21 00:40:05 +00001374 status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
Benny Prijonoaf09dc32007-04-22 12:48:30 +00001375 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
1376 &stun_srv, pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port),
Benny Prijono23674a32007-12-01 08:59:25 +00001377 &p_pub_addr->ipv4);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001378 if (status != PJ_SUCCESS) {
Benny Prijonoebbf6892007-03-24 17:37:25 +00001379 pjsua_perror(THIS_FILE, "Error contacting STUN server", status);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001380 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +00001381 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001382 }
1383
1384 } else {
Benny Prijono23674a32007-12-01 08:59:25 +00001385 pj_bzero(p_pub_addr, sizeof(pj_sockaddr));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001386
Benny Prijono42d08d22007-12-20 11:23:07 +00001387 if (pj_sockaddr_has_addr(&bind_addr)) {
1388 pj_sockaddr_copy_addr(p_pub_addr, &bind_addr);
1389 } else {
1390 status = pj_gethostip(af, p_pub_addr);
1391 if (status != PJ_SUCCESS) {
1392 pjsua_perror(THIS_FILE, "Unable to get local host IP", status);
1393 pj_sock_close(sock);
1394 return status;
1395 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001396 }
1397
Benny Prijono23674a32007-12-01 08:59:25 +00001398 p_pub_addr->addr.sa_family = (pj_uint16_t)af;
1399 pj_sockaddr_set_port(p_pub_addr, (pj_uint16_t)port);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001400 }
1401
1402 *p_sock = sock;
1403
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001404 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
Benny Prijono23674a32007-12-01 08:59:25 +00001405 addr_string(p_pub_addr),
1406 (int)pj_sockaddr_get_port(p_pub_addr)));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001407
Benny Prijonod8410532006-06-15 11:04:33 +00001408 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001409}
1410
1411
1412/*
1413 * Create SIP transport.
1414 */
1415PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
1416 const pjsua_transport_config *cfg,
1417 pjsua_transport_id *p_id)
1418{
1419 pjsip_transport *tp;
1420 unsigned id;
1421 pj_status_t status;
1422
1423 PJSUA_LOCK();
1424
1425 /* Find empty transport slot */
1426 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001427 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001428 break;
1429 }
1430
1431 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1432 status = PJ_ETOOMANY;
1433 pjsua_perror(THIS_FILE, "Error creating transport", status);
1434 goto on_return;
1435 }
1436
1437 /* Create the transport */
Benny Prijonob2477142007-12-05 04:09:59 +00001438 if (type==PJSIP_TRANSPORT_UDP || type==PJSIP_TRANSPORT_UDP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001439 /*
Benny Prijono23674a32007-12-01 08:59:25 +00001440 * Create UDP transport (IPv4 or IPv6).
Benny Prijonoe93e2872006-06-28 16:46:49 +00001441 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001442 pjsua_transport_config config;
Benny Prijono23674a32007-12-01 08:59:25 +00001443 char hostbuf[PJ_INET6_ADDRSTRLEN];
Benny Prijonod8410532006-06-15 11:04:33 +00001444 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijono23674a32007-12-01 08:59:25 +00001445 pj_sockaddr pub_addr;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001446 pjsip_host_port addr_name;
1447
1448 /* Supply default config if it's not specified */
1449 if (cfg == NULL) {
1450 pjsua_transport_config_default(&config);
1451 cfg = &config;
1452 }
1453
Benny Prijono0a5cad82006-09-26 13:21:02 +00001454 /* Initialize the public address from the config, if any */
Benny Prijono23674a32007-12-01 08:59:25 +00001455 pj_sockaddr_init(pjsip_transport_type_get_af(type), &pub_addr,
1456 NULL, (pj_uint16_t)cfg->port);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001457 if (cfg->public_addr.slen) {
Benny Prijono23674a32007-12-01 08:59:25 +00001458 status = pj_sockaddr_set_str_addr(pjsip_transport_type_get_af(type),
1459 &pub_addr, &cfg->public_addr);
Benny Prijono0a5cad82006-09-26 13:21:02 +00001460 if (status != PJ_SUCCESS) {
1461 pjsua_perror(THIS_FILE,
1462 "Unable to resolve transport public address",
1463 status);
1464 goto on_return;
1465 }
1466 }
1467
1468 /* Create the socket and possibly resolve the address with STUN
1469 * (only when public address is not specified).
1470 */
Benny Prijono23674a32007-12-01 08:59:25 +00001471 status = create_sip_udp_sock(pjsip_transport_type_get_af(type),
1472 &cfg->bound_addr, cfg->port,
Benny Prijono0a5cad82006-09-26 13:21:02 +00001473 &sock, &pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001474 if (status != PJ_SUCCESS)
1475 goto on_return;
1476
Benny Prijono23674a32007-12-01 08:59:25 +00001477 pj_ansi_strcpy(hostbuf, addr_string(&pub_addr));
1478 addr_name.host = pj_str(hostbuf);
1479 addr_name.port = pj_sockaddr_get_port(&pub_addr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001480
1481 /* Create UDP transport */
Benny Prijono23674a32007-12-01 08:59:25 +00001482 status = pjsip_udp_transport_attach2(pjsua_var.endpt, type, sock,
1483 &addr_name, 1, &tp);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001484 if (status != PJ_SUCCESS) {
1485 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
1486 status);
1487 pj_sock_close(sock);
1488 goto on_return;
1489 }
1490
Benny Prijonoe93e2872006-06-28 16:46:49 +00001491
1492 /* Save the transport */
1493 pjsua_var.tpdata[id].type = type;
1494 pjsua_var.tpdata[id].local_name = tp->local_name;
1495 pjsua_var.tpdata[id].data.tp = tp;
1496
Benny Prijono3569c0d2007-04-06 10:29:20 +00001497#if defined(PJ_HAS_TCP) && PJ_HAS_TCP!=0
1498
Benny Prijonob2477142007-12-05 04:09:59 +00001499 } else if (type == PJSIP_TRANSPORT_TCP || type == PJSIP_TRANSPORT_TCP6) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001500 /*
1501 * Create TCP transport.
1502 */
1503 pjsua_transport_config config;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001504 pjsip_host_port a_name;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001505 pjsip_tpfactory *tcp;
1506 pj_sockaddr_in local_addr;
1507
1508 /* Supply default config if it's not specified */
1509 if (cfg == NULL) {
1510 pjsua_transport_config_default(&config);
1511 cfg = &config;
1512 }
1513
1514 /* Init local address */
1515 pj_sockaddr_in_init(&local_addr, 0, 0);
1516
1517 if (cfg->port)
1518 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1519
Benny Prijono0a5cad82006-09-26 13:21:02 +00001520 if (cfg->bound_addr.slen) {
1521 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1522 if (status != PJ_SUCCESS) {
1523 pjsua_perror(THIS_FILE,
1524 "Unable to resolve transport bound address",
1525 status);
1526 goto on_return;
1527 }
1528 }
1529
1530 /* Init published name */
1531 pj_bzero(&a_name, sizeof(pjsip_host_port));
1532 if (cfg->public_addr.slen)
1533 a_name.host = cfg->public_addr;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001534
1535 /* Create the TCP transport */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001536 status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr,
1537 &a_name, 1, &tcp);
Benny Prijonoe93e2872006-06-28 16:46:49 +00001538
1539 if (status != PJ_SUCCESS) {
1540 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
1541 status);
1542 goto on_return;
1543 }
1544
1545 /* Save the transport */
1546 pjsua_var.tpdata[id].type = type;
1547 pjsua_var.tpdata[id].local_name = tcp->addr_name;
1548 pjsua_var.tpdata[id].data.factory = tcp;
1549
Benny Prijono3569c0d2007-04-06 10:29:20 +00001550#endif /* PJ_HAS_TCP */
1551
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001552#if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0
1553 } else if (type == PJSIP_TRANSPORT_TLS) {
1554 /*
1555 * Create TLS transport.
1556 */
Benny Prijonof3bbc132006-12-25 06:43:59 +00001557 /*
1558 * Create TCP transport.
1559 */
1560 pjsua_transport_config config;
1561 pjsip_host_port a_name;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001562 pjsip_tpfactory *tls;
Benny Prijonof3bbc132006-12-25 06:43:59 +00001563 pj_sockaddr_in local_addr;
1564
1565 /* Supply default config if it's not specified */
1566 if (cfg == NULL) {
1567 pjsua_transport_config_default(&config);
1568 config.port = 5061;
1569 cfg = &config;
1570 }
1571
1572 /* Init local address */
1573 pj_sockaddr_in_init(&local_addr, 0, 0);
1574
1575 if (cfg->port)
1576 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
1577
1578 if (cfg->bound_addr.slen) {
1579 status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr);
1580 if (status != PJ_SUCCESS) {
1581 pjsua_perror(THIS_FILE,
1582 "Unable to resolve transport bound address",
1583 status);
1584 goto on_return;
1585 }
1586 }
1587
1588 /* Init published name */
1589 pj_bzero(&a_name, sizeof(pjsip_host_port));
1590 if (cfg->public_addr.slen)
1591 a_name.host = cfg->public_addr;
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001592
1593 status = pjsip_tls_transport_start(pjsua_var.endpt,
Benny Prijonof3bbc132006-12-25 06:43:59 +00001594 &cfg->tls_setting,
1595 &local_addr, &a_name, 1, &tls);
Benny Prijono6e0e54b2006-12-08 21:58:31 +00001596 if (status != PJ_SUCCESS) {
1597 pjsua_perror(THIS_FILE, "Error creating SIP TLS listener",
1598 status);
1599 goto on_return;
1600 }
1601
1602 /* Save the transport */
1603 pjsua_var.tpdata[id].type = type;
1604 pjsua_var.tpdata[id].local_name = tls->addr_name;
1605 pjsua_var.tpdata[id].data.factory = tls;
1606#endif
1607
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001608 } else {
1609 status = PJSIP_EUNSUPTRANSPORT;
1610 pjsua_perror(THIS_FILE, "Error creating transport", status);
1611 goto on_return;
1612 }
1613
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001614
1615 /* Return the ID */
1616 if (p_id) *p_id = id;
1617
1618 status = PJ_SUCCESS;
1619
1620on_return:
1621
1622 PJSUA_UNLOCK();
1623
Benny Prijonod8410532006-06-15 11:04:33 +00001624 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001625}
1626
1627
1628/*
1629 * Register transport that has been created by application.
1630 */
1631PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
1632 pjsua_transport_id *p_id)
1633{
1634 unsigned id;
1635
1636 PJSUA_LOCK();
1637
1638 /* Find empty transport slot */
1639 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001640 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001641 break;
1642 }
1643
1644 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
1645 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
1646 PJSUA_UNLOCK();
1647 return PJ_ETOOMANY;
1648 }
1649
1650 /* Save the transport */
Benny Prijonod17a5e92007-05-29 11:51:45 +00001651 pjsua_var.tpdata[id].type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001652 pjsua_var.tpdata[id].local_name = tp->local_name;
1653 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001654
1655 /* Return the ID */
1656 if (p_id) *p_id = id;
1657
1658 PJSUA_UNLOCK();
1659
1660 return PJ_SUCCESS;
1661}
1662
1663
1664/*
1665 * Enumerate all transports currently created in the system.
1666 */
1667PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
1668 unsigned *p_count )
1669{
1670 unsigned i, count;
1671
1672 PJSUA_LOCK();
1673
1674 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1675 ++i)
1676 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001677 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001678 continue;
1679
1680 id[count++] = i;
1681 }
1682
1683 *p_count = count;
1684
1685 PJSUA_UNLOCK();
1686
1687 return PJ_SUCCESS;
1688}
1689
1690
1691/*
1692 * Get information about transports.
1693 */
1694PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1695 pjsua_transport_info *info)
1696{
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001697 pjsua_transport_data *t = &pjsua_var.tpdata[id];
Benny Prijono0a5cad82006-09-26 13:21:02 +00001698 pj_status_t status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001699
Benny Prijonoac623b32006-07-03 15:19:31 +00001700 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001701
1702 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001703 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1704 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001705
1706 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001707 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001708
1709 PJSUA_LOCK();
1710
Benny Prijonoe93e2872006-06-28 16:46:49 +00001711 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1712
1713 pjsip_transport *tp = t->data.tp;
1714
1715 if (tp == NULL) {
1716 PJSUA_UNLOCK();
1717 return PJ_EINVALIDOP;
1718 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001719
Benny Prijonoe93e2872006-06-28 16:46:49 +00001720 info->id = id;
Benny Prijonod17a5e92007-05-29 11:51:45 +00001721 info->type = (pjsip_transport_type_e) tp->key.type;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001722 info->type_name = pj_str(tp->type_name);
1723 info->info = pj_str(tp->info);
1724 info->flag = tp->flag;
1725 info->addr_len = tp->addr_len;
1726 info->local_addr = tp->local_addr;
1727 info->local_name = tp->local_name;
1728 info->usage_count = pj_atomic_get(tp->ref_cnt);
1729
Benny Prijono0a5cad82006-09-26 13:21:02 +00001730 status = PJ_SUCCESS;
1731
Benny Prijonoe93e2872006-06-28 16:46:49 +00001732 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1733
1734 pjsip_tpfactory *factory = t->data.factory;
1735
1736 if (factory == NULL) {
1737 PJSUA_UNLOCK();
1738 return PJ_EINVALIDOP;
1739 }
1740
1741 info->id = id;
1742 info->type = t->type;
1743 info->type_name = pj_str("TCP");
1744 info->info = pj_str("TCP transport");
1745 info->flag = factory->flag;
1746 info->addr_len = sizeof(factory->local_addr);
1747 info->local_addr = factory->local_addr;
1748 info->local_name = factory->addr_name;
1749 info->usage_count = 0;
1750
Benny Prijono0a5cad82006-09-26 13:21:02 +00001751 status = PJ_SUCCESS;
1752
1753 } else {
1754 pj_assert(!"Unsupported transport");
1755 status = PJ_EINVALIDOP;
Benny Prijonoe93e2872006-06-28 16:46:49 +00001756 }
1757
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001758
1759 PJSUA_UNLOCK();
1760
Benny Prijono0a5cad82006-09-26 13:21:02 +00001761 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001762}
1763
1764
1765/*
1766 * Disable a transport or re-enable it.
1767 */
1768PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1769 pj_bool_t enabled)
1770{
1771 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001772 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1773 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001774
1775 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001776 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001777
1778
1779 /* To be done!! */
1780 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001781 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001782
1783 return PJ_EINVALIDOP;
1784}
1785
1786
1787/*
1788 * Close the transport.
1789 */
1790PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1791 pj_bool_t force )
1792{
Benny Prijono5ff61872007-02-01 03:37:11 +00001793 pj_status_t status;
1794
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001795 /* Make sure id is in range. */
Benny Prijonoa1e69682007-05-11 15:14:34 +00001796 PJ_ASSERT_RETURN(id>=0 && id<(int)PJ_ARRAY_SIZE(pjsua_var.tpdata),
1797 PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001798
1799 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001800 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001801
Benny Prijono0a5cad82006-09-26 13:21:02 +00001802 /* Note: destroy() may not work if there are objects still referencing
1803 * the transport.
1804 */
1805 if (force) {
1806 switch (pjsua_var.tpdata[id].type) {
1807 case PJSIP_TRANSPORT_UDP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001808 status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
1809 if (status != PJ_SUCCESS)
1810 return status;
1811 status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp);
1812 if (status != PJ_SUCCESS)
1813 return status;
1814 break;
1815
1816 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001817 case PJSIP_TRANSPORT_TCP:
Benny Prijono5ff61872007-02-01 03:37:11 +00001818 /* This will close the TCP listener, but existing TCP/TLS
1819 * connections (if any) will still linger
1820 */
1821 status = (*pjsua_var.tpdata[id].data.factory->destroy)
1822 (pjsua_var.tpdata[id].data.factory);
1823 if (status != PJ_SUCCESS)
1824 return status;
1825
Benny Prijono0a5cad82006-09-26 13:21:02 +00001826 break;
Benny Prijono5ff61872007-02-01 03:37:11 +00001827
Benny Prijono1ebd6142006-10-19 15:48:02 +00001828 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001829 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001830 }
1831
1832 } else {
Benny Prijono5ff61872007-02-01 03:37:11 +00001833 /* If force is not specified, transports will be closed at their
1834 * convenient time. However this will leak PJSUA-API transport
1835 * descriptors as PJSUA-API wouldn't know when exactly the
1836 * transport is closed thus it can't cleanup PJSUA transport
1837 * descriptor.
1838 */
Benny Prijono0a5cad82006-09-26 13:21:02 +00001839 switch (pjsua_var.tpdata[id].type) {
1840 case PJSIP_TRANSPORT_UDP:
1841 return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp);
Benny Prijono5ff61872007-02-01 03:37:11 +00001842 case PJSIP_TRANSPORT_TLS:
Benny Prijono0a5cad82006-09-26 13:21:02 +00001843 case PJSIP_TRANSPORT_TCP:
1844 return (*pjsua_var.tpdata[id].data.factory->destroy)
1845 (pjsua_var.tpdata[id].data.factory);
Benny Prijono1ebd6142006-10-19 15:48:02 +00001846 default:
Benny Prijono5ff61872007-02-01 03:37:11 +00001847 return PJ_EINVAL;
Benny Prijono0a5cad82006-09-26 13:21:02 +00001848 }
1849 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001850
Benny Prijono5ff61872007-02-01 03:37:11 +00001851 /* Cleanup pjsua data when force is applied */
1852 if (force) {
1853 pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED;
1854 pjsua_var.tpdata[id].data.ptr = NULL;
1855 }
1856
1857 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001858}
1859
1860
1861/*
1862 * Add additional headers etc in msg_data specified by application
1863 * when sending requests.
1864 */
1865void pjsua_process_msg_data(pjsip_tx_data *tdata,
1866 const pjsua_msg_data *msg_data)
1867{
1868 pj_bool_t allow_body;
1869 const pjsip_hdr *hdr;
1870
Benny Prijono56315612006-07-18 14:39:40 +00001871 /* Always add User-Agent */
1872 if (pjsua_var.ua_cfg.user_agent.slen &&
1873 tdata->msg->type == PJSIP_REQUEST_MSG)
1874 {
1875 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1876 pjsip_hdr *h;
1877 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1878 &STR_USER_AGENT,
1879 &pjsua_var.ua_cfg.user_agent);
1880 pjsip_msg_add_hdr(tdata->msg, h);
1881 }
1882
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001883 if (!msg_data)
1884 return;
1885
1886 hdr = msg_data->hdr_list.next;
1887 while (hdr && hdr != &msg_data->hdr_list) {
1888 pjsip_hdr *new_hdr;
1889
Benny Prijonoa1e69682007-05-11 15:14:34 +00001890 new_hdr = (pjsip_hdr*) pjsip_hdr_clone(tdata->pool, hdr);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001891 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1892
1893 hdr = hdr->next;
1894 }
1895
1896 allow_body = (tdata->msg->body == NULL);
1897
1898 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1899 pjsip_media_type ctype;
1900 pjsip_msg_body *body;
1901
1902 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1903 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1904 &msg_data->msg_body);
1905 tdata->msg->body = body;
1906 }
1907}
1908
1909
1910/*
1911 * Add route_set to outgoing requests
1912 */
1913void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1914 const pjsip_route_hdr *route_set )
1915{
1916 const pjsip_route_hdr *r;
1917
1918 r = route_set->next;
1919 while (r != route_set) {
1920 pjsip_route_hdr *new_r;
1921
Benny Prijonoa1e69682007-05-11 15:14:34 +00001922 new_r = (pjsip_route_hdr*) pjsip_hdr_clone(tdata->pool, r);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001923 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1924
1925 r = r->next;
1926 }
1927}
1928
1929
1930/*
1931 * Simple version of MIME type parsing (it doesn't support parameters)
1932 */
1933void pjsua_parse_media_type( pj_pool_t *pool,
1934 const pj_str_t *mime,
1935 pjsip_media_type *media_type)
1936{
1937 pj_str_t tmp;
1938 char *pos;
1939
Benny Prijonoac623b32006-07-03 15:19:31 +00001940 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001941
1942 pj_strdup_with_null(pool, &tmp, mime);
1943
1944 pos = pj_strchr(&tmp, '/');
1945 if (pos) {
1946 media_type->type.ptr = tmp.ptr;
1947 media_type->type.slen = (pos-tmp.ptr);
1948 media_type->subtype.ptr = pos+1;
1949 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1950 } else {
1951 media_type->type = tmp;
1952 }
1953}
1954
1955
1956/*
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001957 * Internal function to init transport selector from transport id.
1958 */
1959void pjsua_init_tpselector(pjsua_transport_id tp_id,
1960 pjsip_tpselector *sel)
1961{
1962 pjsua_transport_data *tpdata;
1963 unsigned flag;
1964
1965 pj_bzero(sel, sizeof(*sel));
1966 if (tp_id == PJSUA_INVALID_ID)
1967 return;
1968
Benny Prijonoa1e69682007-05-11 15:14:34 +00001969 pj_assert(tp_id >= 0 && tp_id < (int)PJ_ARRAY_SIZE(pjsua_var.tpdata));
Benny Prijono62c5c5b2007-01-13 23:22:40 +00001970 tpdata = &pjsua_var.tpdata[tp_id];
1971
1972 flag = pjsip_transport_get_flag_from_type(tpdata->type);
1973
1974 if (flag & PJSIP_TRANSPORT_DATAGRAM) {
1975 sel->type = PJSIP_TPSELECTOR_TRANSPORT;
1976 sel->u.transport = tpdata->data.tp;
1977 } else {
1978 sel->type = PJSIP_TPSELECTOR_LISTENER;
1979 sel->u.listener = tpdata->data.factory;
1980 }
1981}
1982
1983
Benny Prijono6ba8c542007-10-16 01:34:14 +00001984/* Callback upon NAT detection completion */
1985static void nat_detect_cb(void *user_data,
1986 const pj_stun_nat_detect_result *res)
1987{
1988 PJ_UNUSED_ARG(user_data);
1989
1990 pjsua_var.nat_in_progress = PJ_FALSE;
1991 pjsua_var.nat_status = res->status;
1992 pjsua_var.nat_type = res->nat_type;
1993
1994 if (pjsua_var.ua_cfg.cb.on_nat_detect) {
1995 (*pjsua_var.ua_cfg.cb.on_nat_detect)(res);
1996 }
1997}
1998
1999
Benny Prijono62c5c5b2007-01-13 23:22:40 +00002000/*
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002001 * Detect NAT type.
2002 */
Benny Prijono6ba8c542007-10-16 01:34:14 +00002003PJ_DEF(pj_status_t) pjsua_detect_nat_type()
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002004{
2005 pj_status_t status;
2006
Benny Prijono6ba8c542007-10-16 01:34:14 +00002007 if (pjsua_var.nat_in_progress)
2008 return PJ_SUCCESS;
2009
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002010 /* Make sure STUN server resolution has completed */
2011 status = pjsua_resolve_stun_server(PJ_TRUE);
2012 if (status != PJ_SUCCESS) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002013 pjsua_var.nat_status = status;
2014 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002015 return status;
2016 }
2017
2018 /* Make sure we have STUN */
2019 if (pjsua_var.stun_srv.ipv4.sin_family == 0) {
Benny Prijono6ba8c542007-10-16 01:34:14 +00002020 pjsua_var.nat_status = PJNATH_ESTUNINSERVER;
2021 return PJNATH_ESTUNINSERVER;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002022 }
2023
Benny Prijono6ba8c542007-10-16 01:34:14 +00002024 status = pj_stun_detect_nat_type(&pjsua_var.stun_srv.ipv4,
2025 &pjsua_var.stun_cfg,
2026 NULL, &nat_detect_cb);
2027
2028 if (status != PJ_SUCCESS) {
2029 pjsua_var.nat_status = status;
2030 pjsua_var.nat_type = PJ_STUN_NAT_TYPE_ERR_UNKNOWN;
2031 return status;
2032 }
2033
2034 pjsua_var.nat_in_progress = PJ_TRUE;
2035
2036 return PJ_SUCCESS;
2037}
2038
2039
2040/*
2041 * Get NAT type.
2042 */
2043PJ_DEF(pj_status_t) pjsua_get_nat_type(pj_stun_nat_type *type)
2044{
2045 *type = pjsua_var.nat_type;
2046 return pjsua_var.nat_status;
Benny Prijono4ab9fbb2007-10-12 12:14:27 +00002047}
2048
2049
2050/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002051 * Verify that valid SIP url is given.
2052 */
2053PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
2054{
2055 pjsip_uri *p;
2056 pj_pool_t *pool;
2057 char *url;
2058 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
2059
2060 if (!len) return -1;
2061
2062 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
2063 if (!pool) return -1;
2064
Benny Prijonoa1e69682007-05-11 15:14:34 +00002065 url = (char*) pj_pool_alloc(pool, len+1);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002066 pj_ansi_strcpy(url, c_url);
2067
2068 p = pjsip_parse_uri(pool, url, len, 0);
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002069 if (!p || (pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0 &&
2070 pj_stricmp2(pjsip_uri_get_scheme(p), "sips") != 0))
2071 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002072 p = NULL;
Benny Prijonocf0b4b22007-10-06 17:31:09 +00002073 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00002074
2075 pj_pool_release(pool);
2076 return p ? 0 : -1;
2077}
Benny Prijonoda9785b2007-04-02 20:43:06 +00002078
2079
Benny Prijono91d06b62008-09-20 12:16:56 +00002080/**
2081 * Normalize route URI (check for ";lr" and append one if it doesn't
2082 * exist and pjsua_config.force_lr is set.
2083 */
2084pj_status_t normalize_route_uri(pj_pool_t *pool, pj_str_t *uri)
2085{
2086 pj_str_t tmp_uri;
2087 pj_pool_t *tmp_pool;
2088 pjsip_uri *uri_obj;
2089 pjsip_sip_uri *sip_uri;
2090
2091 tmp_pool = pjsua_pool_create("tmplr%p", 512, 512);
2092 if (!tmp_pool)
2093 return PJ_ENOMEM;
2094
2095 pj_strdup_with_null(tmp_pool, &tmp_uri, uri);
2096
2097 uri_obj = pjsip_parse_uri(tmp_pool, tmp_uri.ptr, tmp_uri.slen, 0);
2098 if (!uri_obj) {
2099 PJ_LOG(1,(THIS_FILE, "Invalid route URI: %.*s",
2100 (int)uri->slen, uri->ptr));
2101 pj_pool_release(tmp_pool);
2102 return PJSIP_EINVALIDURI;
2103 }
2104
2105 if (!PJSIP_URI_SCHEME_IS_SIP(uri_obj) &&
2106 !PJSIP_URI_SCHEME_IS_SIP(uri_obj))
2107 {
2108 PJ_LOG(1,(THIS_FILE, "Route URI must be SIP URI: %.*s",
2109 (int)uri->slen, uri->ptr));
2110 pj_pool_release(tmp_pool);
2111 return PJSIP_EINVALIDSCHEME;
2112 }
2113
2114 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri_obj);
2115
2116 /* Done if force_lr is disabled or if lr parameter is present */
2117 if (!pjsua_var.ua_cfg.force_lr || sip_uri->lr_param) {
2118 pj_pool_release(tmp_pool);
2119 return PJ_SUCCESS;
2120 }
2121
2122 /* Set lr param */
2123 sip_uri->lr_param = 1;
2124
2125 /* Print the URI */
2126 tmp_uri.ptr = (char*) pj_pool_alloc(tmp_pool, PJSIP_MAX_URL_SIZE);
2127 tmp_uri.slen = pjsip_uri_print(PJSIP_URI_IN_ROUTING_HDR, uri_obj,
2128 tmp_uri.ptr, PJSIP_MAX_URL_SIZE);
2129 if (tmp_uri.slen < 1) {
2130 PJ_LOG(1,(THIS_FILE, "Route URI is too long: %.*s",
2131 (int)uri->slen, uri->ptr));
2132 pj_pool_release(tmp_pool);
2133 return PJSIP_EURITOOLONG;
2134 }
2135
2136 /* Clone the URI */
2137 pj_strdup_with_null(pool, uri, &tmp_uri);
2138
2139 return PJ_SUCCESS;
2140}
2141
Benny Prijonoda9785b2007-04-02 20:43:06 +00002142/*
2143 * This is a utility function to dump the stack states to log, using
2144 * verbosity level 3.
2145 */
2146PJ_DEF(void) pjsua_dump(pj_bool_t detail)
2147{
2148 unsigned old_decor;
2149 unsigned i;
Benny Prijonoda9785b2007-04-02 20:43:06 +00002150
2151 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
2152
2153 old_decor = pj_log_get_decor();
2154 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
2155
2156 if (detail)
2157 pj_dump_config();
2158
2159 pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail);
2160
2161 pjmedia_endpt_dump(pjsua_get_pjmedia_endpt());
2162
2163 PJ_LOG(3,(THIS_FILE, "Dumping media transports:"));
2164 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2165 pjsua_call *call = &pjsua_var.calls[i];
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002166 pjmedia_transport_info tpinfo;
Benny Prijono5186eae2007-12-03 14:38:25 +00002167 char addr_buf[80];
Benny Prijonoda9785b2007-04-02 20:43:06 +00002168
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002169 /* MSVC complains about tpinfo not being initialized */
Benny Prijono734fc2d2008-03-17 16:05:35 +00002170 //pj_bzero(&tpinfo, sizeof(tpinfo));
Benny Prijono4f093d22007-04-09 08:54:32 +00002171
Benny Prijono734fc2d2008-03-17 16:05:35 +00002172 pjmedia_transport_info_init(&tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002173 pjmedia_transport_get_info(call->med_tp, &tpinfo);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002174
Benny Prijono5186eae2007-12-03 14:38:25 +00002175 PJ_LOG(3,(THIS_FILE, " %s: %s",
Benny Prijonoda9785b2007-04-02 20:43:06 +00002176 (pjsua_var.media_cfg.enable_ice ? "ICE" : "UDP"),
Benny Prijonoe1a5a852008-03-11 21:38:05 +00002177 pj_sockaddr_print(&tpinfo.sock_info.rtp_addr_name, addr_buf,
Benny Prijono5186eae2007-12-03 14:38:25 +00002178 sizeof(addr_buf), 3)));
Benny Prijonoda9785b2007-04-02 20:43:06 +00002179 }
2180
2181 pjsip_tsx_layer_dump(detail);
2182 pjsip_ua_dump(detail);
2183
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002184// Dumping complete call states may require a 'large' buffer
2185// (about 3KB per call session, including RTCP XR).
2186#if 0
Benny Prijonoda9785b2007-04-02 20:43:06 +00002187 /* Dump all invite sessions: */
2188 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
2189
2190 if (pjsua_call_get_count() == 0) {
2191
2192 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
2193
2194 } else {
2195 unsigned i;
2196
2197 for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) {
2198 if (pjsua_call_is_active(i)) {
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002199 /* Tricky logging, since call states log string tends to be
2200 * longer than PJ_LOG_MAX_SIZE.
2201 */
2202 char buf[1024 * 3];
2203 unsigned call_dump_len;
2204 unsigned part_len;
2205 unsigned part_idx;
2206 unsigned log_decor;
2207
Benny Prijonoda9785b2007-04-02 20:43:06 +00002208 pjsua_call_dump(i, detail, buf, sizeof(buf), " ");
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002209 call_dump_len = strlen(buf);
2210
2211 log_decor = pj_log_get_decor();
2212 pj_log_set_decor(log_decor & ~(PJ_LOG_HAS_NEWLINE |
2213 PJ_LOG_HAS_CR));
2214 PJ_LOG(3,(THIS_FILE, "\n"));
2215 pj_log_set_decor(0);
2216
2217 part_idx = 0;
2218 part_len = PJ_LOG_MAX_SIZE-80;
2219 while (part_idx < call_dump_len) {
2220 char p_orig, *p;
2221
2222 p = &buf[part_idx];
2223 if (part_idx + part_len > call_dump_len)
2224 part_len = call_dump_len - part_idx;
2225 p_orig = p[part_len];
2226 p[part_len] = '\0';
2227 PJ_LOG(3,(THIS_FILE, "%s", p));
2228 p[part_len] = p_orig;
2229 part_idx += part_len;
2230 }
2231 pj_log_set_decor(log_decor);
Benny Prijonoda9785b2007-04-02 20:43:06 +00002232 }
2233 }
2234 }
Nanang Izzuddin660eee82008-07-17 14:54:03 +00002235#endif
Benny Prijonoda9785b2007-04-02 20:43:06 +00002236
2237 /* Dump presence status */
2238 pjsua_pres_dump(detail);
2239
2240 pj_log_set_decor(old_decor);
2241 PJ_LOG(3,(THIS_FILE, "Dump complete"));
2242}
2243