Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 4 | * |
| 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 Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 19 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua_internal.h> |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 21 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 22 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 23 | #define THIS_FILE "pjsua_core.c" |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 24 | |
| 25 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 26 | /* PJSUA application instance. */ |
| 27 | struct pjsua_data pjsua_var; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 28 | |
| 29 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 30 | /* Display error */ |
| 31 | PJ_DEF(void) pjsua_perror( const char *sender, const char *title, |
| 32 | pj_status_t status) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 33 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 34 | char errmsg[PJ_ERR_MSG_SIZE]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 35 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 36 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 37 | PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 41 | static void init_data() |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 42 | { |
| 43 | unsigned i; |
| 44 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 45 | pj_bzero(&pjsua_var, sizeof(pjsua_var)); |
| 46 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 47 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.acc); ++i) |
| 48 | pjsua_var.acc[i].index = i; |
| 49 | |
| 50 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata); ++i) |
| 51 | pjsua_var.tpdata[i].index = i; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 52 | |
| 53 | pjsua_var.stun_status = PJ_EUNKNOWN; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 54 | } |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 55 | |
| 56 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 57 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 58 | /***************************************************************************** |
| 59 | * This is a very simple PJSIP module, whose sole purpose is to display |
| 60 | * incoming and outgoing messages to log. This module will have priority |
| 61 | * higher than transport layer, which means: |
| 62 | * |
| 63 | * - incoming messages will come to this module first before reaching |
| 64 | * transaction layer. |
| 65 | * |
| 66 | * - outgoing messages will come to this module last, after the message |
| 67 | * has been 'printed' to contiguous buffer by transport layer and |
| 68 | * appropriate transport instance has been decided for this message. |
| 69 | * |
| 70 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 71 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 72 | /* Notification on incoming messages */ |
| 73 | static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata) |
| 74 | { |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 75 | PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n" |
| 76 | "%.*s\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 77 | "--end msg--", |
| 78 | rdata->msg_info.len, |
| 79 | pjsip_rx_data_get_info(rdata), |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 80 | rdata->tp_info.transport->type_name, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 81 | rdata->pkt_info.src_name, |
| 82 | rdata->pkt_info.src_port, |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 83 | (int)rdata->msg_info.len, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 84 | rdata->msg_info.msg_buf)); |
| 85 | |
| 86 | /* Always return false, otherwise messages will not get processed! */ |
| 87 | return PJ_FALSE; |
| 88 | } |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 89 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 90 | /* Notification on outgoing messages */ |
| 91 | static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) |
| 92 | { |
| 93 | |
| 94 | /* Important note: |
| 95 | * tp_info field is only valid after outgoing messages has passed |
| 96 | * transport layer. So don't try to access tp_info when the module |
| 97 | * has lower priority than transport layer. |
| 98 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 99 | |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 100 | PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n" |
| 101 | "%.*s\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 102 | "--end msg--", |
| 103 | (tdata->buf.cur - tdata->buf.start), |
| 104 | pjsip_tx_data_get_info(tdata), |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 105 | tdata->tp_info.transport->type_name, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 106 | tdata->tp_info.dst_name, |
| 107 | tdata->tp_info.dst_port, |
Benny Prijono | b4a17c9 | 2006-07-10 14:40:21 +0000 | [diff] [blame] | 108 | (int)(tdata->buf.cur - tdata->buf.start), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 109 | tdata->buf.start)); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 110 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 111 | /* Always return success, otherwise message will not get sent! */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 112 | return PJ_SUCCESS; |
| 113 | } |
| 114 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 115 | /* The module instance. */ |
| 116 | static pjsip_module pjsua_msg_logger = |
| 117 | { |
| 118 | NULL, NULL, /* prev, next. */ |
| 119 | { "mod-pjsua-log", 13 }, /* Name. */ |
| 120 | -1, /* Id */ |
| 121 | PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */ |
| 122 | NULL, /* load() */ |
| 123 | NULL, /* start() */ |
| 124 | NULL, /* stop() */ |
| 125 | NULL, /* unload() */ |
| 126 | &logging_on_rx_msg, /* on_rx_request() */ |
| 127 | &logging_on_rx_msg, /* on_rx_response() */ |
| 128 | &logging_on_tx_msg, /* on_tx_request. */ |
| 129 | &logging_on_tx_msg, /* on_tx_response() */ |
| 130 | NULL, /* on_tsx_state() */ |
| 131 | |
| 132 | }; |
| 133 | |
| 134 | |
| 135 | /***************************************************************************** |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 136 | * Another simple module to handle incoming OPTIONS request |
| 137 | */ |
| 138 | |
| 139 | /* Notification on incoming request */ |
| 140 | static pj_bool_t options_on_rx_request(pjsip_rx_data *rdata) |
| 141 | { |
| 142 | pjsip_tx_data *tdata; |
| 143 | pjsip_response_addr res_addr; |
| 144 | pjmedia_sdp_session *sdp; |
| 145 | const pjsip_hdr *cap_hdr; |
| 146 | pj_status_t status; |
| 147 | |
| 148 | /* Only want to handle OPTIONS requests */ |
| 149 | if (pjsip_method_cmp(&rdata->msg_info.msg->line.req.method, |
| 150 | &pjsip_options_method) != 0) |
| 151 | { |
| 152 | return PJ_FALSE; |
| 153 | } |
| 154 | |
| 155 | /* Create basic response. */ |
| 156 | status = pjsip_endpt_create_response(pjsua_var.endpt, rdata, 200, NULL, |
| 157 | &tdata); |
| 158 | if (status != PJ_SUCCESS) { |
| 159 | pjsua_perror(THIS_FILE, "Unable to create OPTIONS response", status); |
| 160 | return PJ_TRUE; |
| 161 | } |
| 162 | |
| 163 | /* Add Allow header */ |
| 164 | cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ALLOW, NULL); |
| 165 | if (cap_hdr) { |
| 166 | pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 167 | } |
| 168 | |
| 169 | /* Add Accept header */ |
| 170 | cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_ACCEPT, NULL); |
| 171 | if (cap_hdr) { |
| 172 | pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 173 | } |
| 174 | |
| 175 | /* Add Supported header */ |
| 176 | cap_hdr = pjsip_endpt_get_capability(pjsua_var.endpt, PJSIP_H_SUPPORTED, NULL); |
| 177 | if (cap_hdr) { |
| 178 | pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 179 | } |
| 180 | |
| 181 | /* Add Allow-Events header from the evsub module */ |
| 182 | cap_hdr = pjsip_evsub_get_allow_events_hdr(NULL); |
| 183 | if (cap_hdr) { |
| 184 | pjsip_msg_add_hdr(tdata->msg, pjsip_hdr_clone(tdata->pool, cap_hdr)); |
| 185 | } |
| 186 | |
| 187 | /* Add User-Agent header */ |
| 188 | if (pjsua_var.ua_cfg.user_agent.slen) { |
| 189 | const pj_str_t USER_AGENT = { "User-Agent", 10}; |
| 190 | pjsip_hdr *h; |
| 191 | |
| 192 | h = (pjsip_hdr*) pjsip_generic_string_hdr_create(tdata->pool, |
| 193 | &USER_AGENT, |
| 194 | &pjsua_var.ua_cfg.user_agent); |
| 195 | pjsip_msg_add_hdr(tdata->msg, h); |
| 196 | } |
| 197 | |
| 198 | /* Add SDP body, using call0's RTP address */ |
| 199 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, 1, |
| 200 | &pjsua_var.calls[0].skinfo, &sdp); |
| 201 | if (status == PJ_SUCCESS) { |
| 202 | pjsip_create_sdp_body(tdata->pool, sdp, &tdata->msg->body); |
| 203 | } |
| 204 | |
| 205 | /* Send response statelessly */ |
| 206 | pjsip_get_response_addr(tdata->pool, rdata, &res_addr); |
| 207 | status = pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, tdata, NULL, NULL); |
| 208 | if (status != PJ_SUCCESS) |
| 209 | pjsip_tx_data_dec_ref(tdata); |
| 210 | |
| 211 | return PJ_TRUE; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | /* The module instance. */ |
| 216 | static pjsip_module pjsua_options_handler = |
| 217 | { |
| 218 | NULL, NULL, /* prev, next. */ |
| 219 | { "mod-pjsua-options", 17 }, /* Name. */ |
| 220 | -1, /* Id */ |
| 221 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 222 | NULL, /* load() */ |
| 223 | NULL, /* start() */ |
| 224 | NULL, /* stop() */ |
| 225 | NULL, /* unload() */ |
| 226 | &options_on_rx_request, /* on_rx_request() */ |
| 227 | NULL, /* on_rx_response() */ |
| 228 | NULL, /* on_tx_request. */ |
| 229 | NULL, /* on_tx_response() */ |
| 230 | NULL, /* on_tsx_state() */ |
| 231 | |
| 232 | }; |
| 233 | |
| 234 | |
| 235 | /***************************************************************************** |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 236 | * These two functions are the main callbacks registered to PJSIP stack |
| 237 | * to receive SIP request and response messages that are outside any |
| 238 | * dialogs and any transactions. |
| 239 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 240 | |
| 241 | /* |
| 242 | * Handler for receiving incoming requests. |
| 243 | * |
| 244 | * This handler serves multiple purposes: |
| 245 | * - it receives requests outside dialogs. |
| 246 | * - it receives requests inside dialogs, when the requests are |
| 247 | * unhandled by other dialog usages. Example of these |
| 248 | * requests are: MESSAGE. |
| 249 | */ |
| 250 | static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata) |
| 251 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 252 | pj_bool_t processed = PJ_FALSE; |
| 253 | |
| 254 | PJSUA_LOCK(); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 255 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 256 | if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 257 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 258 | processed = pjsua_call_on_incoming(rdata); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 261 | PJSUA_UNLOCK(); |
| 262 | |
| 263 | return processed; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
| 267 | /* |
| 268 | * Handler for receiving incoming responses. |
| 269 | * |
| 270 | * This handler serves multiple purposes: |
| 271 | * - it receives strayed responses (i.e. outside any dialog and |
| 272 | * outside any transactions). |
| 273 | * - it receives responses coming to a transaction, when pjsua |
| 274 | * module is set as transaction user for the transaction. |
| 275 | * - it receives responses inside a dialog, when these responses |
| 276 | * are unhandled by other dialog usages. |
| 277 | */ |
| 278 | static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata) |
| 279 | { |
| 280 | PJ_UNUSED_ARG(rdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 281 | return PJ_FALSE; |
| 282 | } |
| 283 | |
| 284 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 285 | /***************************************************************************** |
| 286 | * Logging. |
| 287 | */ |
| 288 | |
| 289 | /* Log callback */ |
| 290 | static void log_writer(int level, const char *buffer, int len) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 291 | { |
Benny Prijono | 572d485 | 2006-11-23 21:50:02 +0000 | [diff] [blame] | 292 | /* Write to file, stdout or application callback. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 293 | |
| 294 | if (pjsua_var.log_file) { |
| 295 | pj_ssize_t size = len; |
| 296 | pj_file_write(pjsua_var.log_file, buffer, &size); |
Benny Prijono | ee73871 | 2007-03-16 09:25:47 +0000 | [diff] [blame] | 297 | pj_file_flush(pjsua_var.log_file); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Benny Prijono | 572d485 | 2006-11-23 21:50:02 +0000 | [diff] [blame] | 300 | if (level <= (int)pjsua_var.log_cfg.console_level) { |
| 301 | if (pjsua_var.log_cfg.cb) |
| 302 | (*pjsua_var.log_cfg.cb)(level, buffer, len); |
| 303 | else |
| 304 | pj_log_write(level, buffer, len); |
| 305 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | /* |
| 310 | * Application can call this function at any time (after pjsua_create(), of |
| 311 | * course) to change logging settings. |
| 312 | */ |
| 313 | PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg) |
| 314 | { |
| 315 | pj_status_t status; |
| 316 | |
| 317 | /* Save config. */ |
| 318 | pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg); |
| 319 | |
| 320 | /* Redirect log function to ours */ |
| 321 | pj_log_set_log_func( &log_writer ); |
| 322 | |
| 323 | /* Close existing file, if any */ |
| 324 | if (pjsua_var.log_file) { |
| 325 | pj_file_close(pjsua_var.log_file); |
| 326 | pjsua_var.log_file = NULL; |
| 327 | } |
| 328 | |
| 329 | /* If output log file is desired, create the file: */ |
| 330 | if (pjsua_var.log_cfg.log_filename.slen) { |
| 331 | |
| 332 | status = pj_file_open(pjsua_var.pool, |
| 333 | pjsua_var.log_cfg.log_filename.ptr, |
| 334 | PJ_O_WRONLY, |
| 335 | &pjsua_var.log_file); |
| 336 | |
| 337 | if (status != PJ_SUCCESS) { |
| 338 | pjsua_perror(THIS_FILE, "Error creating log file", status); |
| 339 | return status; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /* Unregister msg logging if it's previously registered */ |
| 344 | if (pjsua_msg_logger.id >= 0) { |
| 345 | pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 346 | pjsua_msg_logger.id = -1; |
| 347 | } |
| 348 | |
| 349 | /* Enable SIP message logging */ |
| 350 | if (pjsua_var.log_cfg.msg_logging) |
| 351 | pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 352 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 353 | return PJ_SUCCESS; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | /***************************************************************************** |
| 358 | * PJSUA Base API. |
| 359 | */ |
| 360 | |
| 361 | /* Worker thread function. */ |
| 362 | static int worker_thread(void *arg) |
| 363 | { |
| 364 | enum { TIMEOUT = 10 }; |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 365 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 366 | PJ_UNUSED_ARG(arg); |
| 367 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 368 | while (!pjsua_var.thread_quit_flag) { |
| 369 | int count; |
| 370 | |
| 371 | count = pjsua_handle_events(TIMEOUT); |
| 372 | if (count < 0) |
| 373 | pj_thread_sleep(TIMEOUT); |
| 374 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 379 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 380 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 381 | * Instantiate pjsua application. |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 382 | */ |
| 383 | PJ_DEF(pj_status_t) pjsua_create(void) |
| 384 | { |
| 385 | pj_status_t status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 386 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 387 | /* Init pjsua data */ |
| 388 | init_data(); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 389 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 390 | /* Set default logging settings */ |
| 391 | pjsua_logging_config_default(&pjsua_var.log_cfg); |
| 392 | |
| 393 | /* Init PJLIB: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 394 | status = pj_init(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 395 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 396 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 397 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 398 | /* Init PJLIB-UTIL: */ |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 399 | status = pjlib_util_init(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 400 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 401 | |
Benny Prijono | ec92134 | 2007-03-24 13:00:30 +0000 | [diff] [blame] | 402 | /* Init PJNATH */ |
| 403 | status = pjnath_init(); |
| 404 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 405 | |
Benny Prijono | 094d3ad | 2006-11-21 08:41:00 +0000 | [diff] [blame] | 406 | /* Set default sound device ID */ |
| 407 | pjsua_var.cap_dev = pjsua_var.play_dev = -1; |
| 408 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 409 | /* Init caching pool. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 410 | pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 411 | |
| 412 | /* Create memory pool for application. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 413 | pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000); |
| 414 | |
| 415 | PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 416 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 417 | /* Create mutex */ |
| 418 | status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua", |
| 419 | &pjsua_var.mutex); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 420 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 421 | pjsua_perror(THIS_FILE, "Unable to create mutex", status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 422 | return status; |
| 423 | } |
| 424 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 425 | /* Must create SIP endpoint to initialize SIP parser. The parser |
| 426 | * is needed for example when application needs to call pjsua_verify_url(). |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 427 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 428 | status = pjsip_endpt_create(&pjsua_var.cp.factory, |
| 429 | pj_gethostname()->ptr, |
| 430 | &pjsua_var.endpt); |
| 431 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 432 | |
| 433 | |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 434 | return PJ_SUCCESS; |
| 435 | } |
| 436 | |
| 437 | |
| 438 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 439 | * Initialize pjsua with the specified settings. All the settings are |
| 440 | * optional, and the default values will be used when the config is not |
| 441 | * specified. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 442 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 443 | PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg, |
| 444 | const pjsua_logging_config *log_cfg, |
| 445 | const pjsua_media_config *media_cfg) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 446 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 447 | pjsua_config default_cfg; |
| 448 | pjsua_media_config default_media_cfg; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 449 | const pj_str_t STR_OPTIONS = { "OPTIONS", 7 }; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 450 | pj_status_t status; |
| 451 | |
| 452 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 453 | /* Create default configurations when the config is not supplied */ |
| 454 | |
| 455 | if (ua_cfg == NULL) { |
| 456 | pjsua_config_default(&default_cfg); |
| 457 | ua_cfg = &default_cfg; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 460 | if (media_cfg == NULL) { |
| 461 | pjsua_media_config_default(&default_media_cfg); |
| 462 | media_cfg = &default_media_cfg; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 465 | /* Initialize logging first so that info/errors can be captured */ |
| 466 | if (log_cfg) { |
| 467 | status = pjsua_reconfigure_logging(log_cfg); |
| 468 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 471 | /* If nameserver is configured, create DNS resolver instance and |
| 472 | * set it to be used by SIP resolver. |
| 473 | */ |
| 474 | if (ua_cfg->nameserver_count) { |
| 475 | #if PJSIP_HAS_RESOLVER |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 476 | unsigned i; |
| 477 | |
| 478 | /* Create DNS resolver */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 479 | status = pjsip_endpt_create_resolver(pjsua_var.endpt, |
| 480 | &pjsua_var.resolver); |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 481 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 482 | |
| 483 | /* Configure nameserver for the DNS resolver */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 484 | status = pj_dns_resolver_set_ns(pjsua_var.resolver, |
| 485 | ua_cfg->nameserver_count, |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 486 | ua_cfg->nameserver, NULL); |
| 487 | if (status != PJ_SUCCESS) { |
| 488 | pjsua_perror(THIS_FILE, "Error setting nameserver", status); |
| 489 | return status; |
| 490 | } |
| 491 | |
| 492 | /* Set this DNS resolver to be used by the SIP resolver */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 493 | status = pjsip_endpt_set_resolver(pjsua_var.endpt, pjsua_var.resolver); |
Benny Prijono | fa9e5b1 | 2006-10-08 12:39:34 +0000 | [diff] [blame] | 494 | if (status != PJ_SUCCESS) { |
| 495 | pjsua_perror(THIS_FILE, "Error setting DNS resolver", status); |
| 496 | return status; |
| 497 | } |
| 498 | |
| 499 | /* Print nameservers */ |
| 500 | for (i=0; i<ua_cfg->nameserver_count; ++i) { |
| 501 | PJ_LOG(4,(THIS_FILE, "Nameserver %.*s added", |
| 502 | (int)ua_cfg->nameserver[i].slen, |
| 503 | ua_cfg->nameserver[i].ptr)); |
| 504 | } |
| 505 | #else |
| 506 | PJ_LOG(2,(THIS_FILE, |
| 507 | "DNS resolver is disabled (PJSIP_HAS_RESOLVER==0)")); |
| 508 | #endif |
| 509 | } |
| 510 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 511 | /* Init SIP UA: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 512 | |
| 513 | /* Initialize transaction layer: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 514 | status = pjsip_tsx_layer_init_module(pjsua_var.endpt); |
| 515 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 516 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 517 | |
| 518 | /* Initialize UA layer module: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 519 | status = pjsip_ua_init_module( pjsua_var.endpt, NULL ); |
| 520 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 521 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 522 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 523 | /* Initialize Replaces support. */ |
| 524 | status = pjsip_replaces_init_module( pjsua_var.endpt ); |
| 525 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 526 | |
| 527 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 528 | /* Initialize and register PJSUA application module. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 529 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 530 | const pjsip_module mod_initializer = |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 531 | { |
| 532 | NULL, NULL, /* prev, next. */ |
| 533 | { "mod-pjsua", 9 }, /* Name. */ |
| 534 | -1, /* Id */ |
| 535 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 536 | NULL, /* load() */ |
| 537 | NULL, /* start() */ |
| 538 | NULL, /* stop() */ |
| 539 | NULL, /* unload() */ |
| 540 | &mod_pjsua_on_rx_request, /* on_rx_request() */ |
| 541 | &mod_pjsua_on_rx_response, /* on_rx_response() */ |
| 542 | NULL, /* on_tx_request. */ |
| 543 | NULL, /* on_tx_response() */ |
| 544 | NULL, /* on_tsx_state() */ |
| 545 | }; |
| 546 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 547 | pjsua_var.mod = mod_initializer; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 548 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 549 | status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod); |
| 550 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 553 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 554 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 555 | /* Initialize PJSUA call subsystem: */ |
| 556 | status = pjsua_call_subsys_init(ua_cfg); |
| 557 | if (status != PJ_SUCCESS) |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 558 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 559 | |
| 560 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 561 | /* Start resolving STUN server */ |
| 562 | status = pjsua_resolve_stun_server(PJ_FALSE); |
| 563 | if (status != PJ_SUCCESS && status != PJ_EPENDING) { |
| 564 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 565 | return status; |
| 566 | } |
| 567 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 568 | /* Initialize PJSUA media subsystem */ |
| 569 | status = pjsua_media_subsys_init(media_cfg); |
| 570 | if (status != PJ_SUCCESS) |
| 571 | goto on_error; |
| 572 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 573 | |
| 574 | /* Init core SIMPLE module : */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 575 | status = pjsip_evsub_init_module(pjsua_var.endpt); |
| 576 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 577 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 578 | |
| 579 | /* Init presence module: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 580 | status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance()); |
| 581 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 582 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 583 | /* Init PUBLISH module */ |
| 584 | pjsip_publishc_init_module(pjsua_var.endpt); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 585 | |
| 586 | /* Init xfer/REFER module */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 587 | status = pjsip_xfer_init_module( pjsua_var.endpt ); |
| 588 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 589 | |
| 590 | /* Init pjsua presence handler: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 591 | status = pjsua_pres_init(); |
| 592 | if (status != PJ_SUCCESS) |
| 593 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 594 | |
| 595 | /* Init out-of-dialog MESSAGE request handler. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 596 | status = pjsua_im_init(); |
| 597 | if (status != PJ_SUCCESS) |
| 598 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 599 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 600 | /* Register OPTIONS handler */ |
| 601 | pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler); |
| 602 | |
| 603 | /* Add OPTIONS in Allow header */ |
| 604 | pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW, |
| 605 | NULL, 1, &STR_OPTIONS); |
| 606 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 607 | /* Start worker thread if needed. */ |
| 608 | if (pjsua_var.ua_cfg.thread_cnt) { |
| 609 | unsigned i; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 610 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 611 | if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread)) |
| 612 | pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 613 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 614 | for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 615 | status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread, |
| 616 | NULL, 0, 0, &pjsua_var.thread[i]); |
| 617 | if (status != PJ_SUCCESS) |
| 618 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 619 | } |
Benny Prijono | f521eb0 | 2006-08-06 23:07:25 +0000 | [diff] [blame] | 620 | PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created", |
| 621 | pjsua_var.ua_cfg.thread_cnt)); |
| 622 | } else { |
| 623 | PJ_LOG(4,(THIS_FILE, "No SIP worker threads created")); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 626 | /* Done! */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 627 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 628 | PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized", |
| 629 | PJ_VERSION, PJ_OS_NAME)); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 630 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 631 | return PJ_SUCCESS; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 632 | |
| 633 | on_error: |
| 634 | pjsua_destroy(); |
| 635 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 639 | /* Sleep with polling */ |
| 640 | static void busy_sleep(unsigned msec) |
| 641 | { |
| 642 | pj_time_val timeout, now; |
| 643 | |
| 644 | pj_gettimeofday(&timeout); |
| 645 | timeout.msec += msec; |
| 646 | pj_time_val_normalize(&timeout); |
| 647 | |
| 648 | do { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 649 | while (pjsua_handle_events(10) > 0) |
| 650 | ; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 651 | pj_gettimeofday(&now); |
| 652 | } while (PJ_TIME_VAL_LT(now, timeout)); |
| 653 | } |
| 654 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 655 | |
| 656 | static pj_status_t resolve_stun_server(pj_bool_t use_dns_srv); |
| 657 | |
| 658 | /* |
| 659 | * Callback function to receive notification from the resolver |
| 660 | * when the resolution process completes. |
| 661 | */ |
| 662 | static void stun_dns_srv_resolver_cb(void *user_data, |
| 663 | pj_status_t status, |
| 664 | const pj_dns_srv_record *rec) |
| 665 | { |
| 666 | unsigned i; |
| 667 | |
| 668 | PJ_UNUSED_ARG(user_data); |
| 669 | |
| 670 | pjsua_var.stun_status = status; |
| 671 | |
| 672 | if (status != PJ_SUCCESS) { |
| 673 | /* DNS SRV resolution failed. If stun_host is specified, resolve |
| 674 | * it with gethostbyname() |
| 675 | */ |
| 676 | if (pjsua_var.ua_cfg.stun_host.slen) { |
| 677 | pj_hostent he; |
| 678 | |
| 679 | pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he); |
| 680 | |
| 681 | if (pjsua_var.stun_status == PJ_SUCCESS) { |
| 682 | pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0); |
| 683 | pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr; |
| 684 | pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478); |
| 685 | |
| 686 | PJ_LOG(3,(THIS_FILE, |
| 687 | "STUN server %.*s resolved, address is %s:%d", |
| 688 | (int)pjsua_var.ua_cfg.stun_host.slen, |
| 689 | pjsua_var.ua_cfg.stun_host.ptr, |
| 690 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr), |
| 691 | (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port))); |
| 692 | } |
| 693 | } else { |
| 694 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 695 | |
| 696 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 697 | PJ_LOG(1,(THIS_FILE, |
| 698 | "DNS SRV resolution failed for STUN server %.*s: %s", |
| 699 | (int)pjsua_var.ua_cfg.stun_domain.slen, |
| 700 | pjsua_var.ua_cfg.stun_domain.ptr, |
| 701 | errmsg)); |
| 702 | } |
| 703 | return; |
| 704 | } |
| 705 | |
| 706 | pj_memcpy(&pjsua_var.stun_srv, &rec->entry[0].addr, |
| 707 | rec->entry[0].addr_len); |
| 708 | |
| 709 | PJ_LOG(3,(THIS_FILE, "_stun._udp.%.*s resolved, found %d entry(s):", |
| 710 | (int)pjsua_var.ua_cfg.stun_domain.slen, |
| 711 | pjsua_var.ua_cfg.stun_domain.ptr, |
| 712 | rec->count)); |
| 713 | |
| 714 | for (i=0; i<rec->count; ++i) { |
| 715 | PJ_LOG(3,(THIS_FILE, |
| 716 | " %d: prio=%d, weight=%d %s:%d", |
| 717 | i, rec->entry[i].priority, rec->entry[i].weight, |
| 718 | pj_inet_ntoa(rec->entry[i].addr.ipv4.sin_addr), |
| 719 | (int)pj_ntohs(rec->entry[i].addr.ipv4.sin_port))); |
| 720 | } |
| 721 | |
| 722 | } |
| 723 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 724 | /* |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 725 | * Resolve STUN server. |
| 726 | */ |
| 727 | pj_status_t pjsua_resolve_stun_server(pj_bool_t wait) |
| 728 | { |
| 729 | if (pjsua_var.stun_status == PJ_EUNKNOWN) { |
| 730 | /* Initialize STUN configuration */ |
| 731 | pj_stun_config_init(&pjsua_var.stun_cfg, &pjsua_var.cp.factory, 0, |
| 732 | pjsip_endpt_get_ioqueue(pjsua_var.endpt), |
| 733 | pjsip_endpt_get_timer_heap(pjsua_var.endpt)); |
| 734 | |
| 735 | /* Start STUN server resolution */ |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 736 | |
| 737 | pjsua_var.stun_status = PJ_EPENDING; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 738 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 739 | /* If stun_domain is specified, resolve STUN servers with DNS |
| 740 | * SRV resolution. |
| 741 | */ |
| 742 | if (pjsua_var.ua_cfg.stun_domain.slen) { |
| 743 | pj_str_t res_type; |
| 744 | |
| 745 | /* Fail if resolver is not configured */ |
| 746 | if (pjsua_var.resolver == NULL) { |
| 747 | PJ_LOG(1,(THIS_FILE, "Nameserver must be configured when " |
| 748 | "stun_domain is specified")); |
| 749 | pjsua_var.stun_status = PJLIB_UTIL_EDNSNONS; |
| 750 | return PJLIB_UTIL_EDNSNONS; |
| 751 | } |
| 752 | res_type = pj_str("_stun._udp"); |
| 753 | pjsua_var.stun_status = |
| 754 | pj_dns_srv_resolve(&pjsua_var.ua_cfg.stun_domain, &res_type, |
| 755 | 3478, pjsua_var.pool, pjsua_var.resolver, |
| 756 | 0, NULL, stun_dns_srv_resolver_cb); |
| 757 | if (pjsua_var.stun_status != PJ_SUCCESS) { |
| 758 | pjsua_perror(THIS_FILE, "Error starting DNS SRV resolution", |
| 759 | pjsua_var.stun_status); |
| 760 | return pjsua_var.stun_status; |
| 761 | } |
| 762 | } |
| 763 | /* Otherwise if stun_host is specified, resolve STUN server with |
| 764 | * gethostbyname(). |
| 765 | */ |
| 766 | else if (pjsua_var.ua_cfg.stun_host.slen) { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 767 | pj_hostent he; |
| 768 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 769 | pjsua_var.stun_status = pj_gethostbyname(&pjsua_var.ua_cfg.stun_host, &he); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 770 | |
| 771 | if (pjsua_var.stun_status == PJ_SUCCESS) { |
| 772 | pj_sockaddr_in_init(&pjsua_var.stun_srv.ipv4, NULL, 0); |
| 773 | pjsua_var.stun_srv.ipv4.sin_addr = *(pj_in_addr*)he.h_addr; |
| 774 | pjsua_var.stun_srv.ipv4.sin_port = pj_htons((pj_uint16_t)3478); |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 775 | |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 776 | PJ_LOG(3,(THIS_FILE, |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 777 | "STUN server %.*s resolved, address is %s:%d", |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 778 | (int)pjsua_var.ua_cfg.stun_host.slen, |
| 779 | pjsua_var.ua_cfg.stun_host.ptr, |
Benny Prijono | f75eceb | 2007-03-23 19:09:54 +0000 | [diff] [blame] | 780 | pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr), |
| 781 | (int)pj_ntohs(pjsua_var.stun_srv.ipv4.sin_port))); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 782 | } |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 783 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 784 | } |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 785 | /* Otherwise disable STUN. */ |
| 786 | else { |
| 787 | pjsua_var.stun_status = PJ_SUCCESS; |
| 788 | } |
| 789 | |
| 790 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 791 | return pjsua_var.stun_status; |
| 792 | |
| 793 | } else if (pjsua_var.stun_status == PJ_EPENDING) { |
| 794 | /* STUN server resolution has been started, wait for the |
| 795 | * result. |
| 796 | */ |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 797 | if (wait) { |
| 798 | while (pjsua_var.stun_status == PJ_EPENDING) |
| 799 | pjsua_handle_events(10); |
| 800 | } |
| 801 | |
| 802 | return pjsua_var.stun_status; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 803 | |
| 804 | } else { |
| 805 | /* STUN server has been resolved, return the status */ |
| 806 | return pjsua_var.stun_status; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | /* |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 811 | * Destroy pjsua. |
| 812 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 813 | PJ_DEF(pj_status_t) pjsua_destroy(void) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 814 | { |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 815 | int i; /* Must be signed */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 816 | |
| 817 | /* Signal threads to quit: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 818 | pjsua_var.thread_quit_flag = 1; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 819 | |
| 820 | /* Wait worker threads to quit: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 821 | for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 822 | if (pjsua_var.thread[i]) { |
| 823 | pj_thread_join(pjsua_var.thread[i]); |
| 824 | pj_thread_destroy(pjsua_var.thread[i]); |
| 825 | pjsua_var.thread[i] = NULL; |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 826 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 827 | } |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 828 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 829 | if (pjsua_var.endpt) { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 830 | /* Terminate all calls. */ |
| 831 | pjsua_call_hangup_all(); |
| 832 | |
| 833 | /* Terminate all presence subscriptions. */ |
| 834 | pjsua_pres_shutdown(); |
| 835 | |
| 836 | /* Unregister, if required: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 837 | for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 838 | if (!pjsua_var.acc[i].valid) |
| 839 | continue; |
| 840 | |
| 841 | if (pjsua_var.acc[i].regc) { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 842 | pjsua_acc_set_registration(i, PJ_FALSE); |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | /* Wait for some time to allow unregistration to complete: */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 847 | PJ_LOG(4,(THIS_FILE, "Shutting down...")); |
| 848 | busy_sleep(1000); |
| 849 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 850 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 851 | /* Destroy media */ |
| 852 | pjsua_media_subsys_destroy(); |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 853 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 854 | /* Destroy endpoint. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 855 | if (pjsua_var.endpt) { |
| 856 | pjsip_endpt_destroy(pjsua_var.endpt); |
| 857 | pjsua_var.endpt = NULL; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 858 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 859 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 860 | /* Destroy mutex */ |
| 861 | if (pjsua_var.mutex) { |
| 862 | pj_mutex_destroy(pjsua_var.mutex); |
| 863 | pjsua_var.mutex = NULL; |
| 864 | } |
| 865 | |
| 866 | /* Destroy pool and pool factory. */ |
| 867 | if (pjsua_var.pool) { |
| 868 | pj_pool_release(pjsua_var.pool); |
| 869 | pjsua_var.pool = NULL; |
| 870 | pj_caching_pool_destroy(&pjsua_var.cp); |
| 871 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 872 | |
| 873 | |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 874 | PJ_LOG(4,(THIS_FILE, "PJSUA destroyed...")); |
| 875 | |
| 876 | /* End logging */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 877 | if (pjsua_var.log_file) { |
| 878 | pj_file_close(pjsua_var.log_file); |
| 879 | pjsua_var.log_file = NULL; |
| 880 | } |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 881 | |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 882 | /* Shutdown PJLIB */ |
| 883 | pj_shutdown(); |
| 884 | |
Benny Prijono | f762ee7 | 2006-12-01 11:14:37 +0000 | [diff] [blame] | 885 | /* Clear pjsua_var */ |
| 886 | pj_bzero(&pjsua_var, sizeof(pjsua_var)); |
| 887 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 888 | /* Done. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 889 | return PJ_SUCCESS; |
| 890 | } |
| 891 | |
| 892 | |
| 893 | /** |
| 894 | * Application is recommended to call this function after all initialization |
| 895 | * is done, so that the library can do additional checking set up |
| 896 | * additional |
| 897 | * |
| 898 | * @return PJ_SUCCESS on success, or the appropriate error code. |
| 899 | */ |
| 900 | PJ_DEF(pj_status_t) pjsua_start(void) |
| 901 | { |
| 902 | pj_status_t status; |
| 903 | |
| 904 | status = pjsua_call_subsys_start(); |
| 905 | if (status != PJ_SUCCESS) |
| 906 | return status; |
| 907 | |
| 908 | status = pjsua_media_subsys_start(); |
| 909 | if (status != PJ_SUCCESS) |
| 910 | return status; |
| 911 | |
| 912 | status = pjsua_pres_start(); |
| 913 | if (status != PJ_SUCCESS) |
| 914 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 915 | |
| 916 | return PJ_SUCCESS; |
| 917 | } |
| 918 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 919 | |
| 920 | /** |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 921 | * Poll pjsua for events, and if necessary block the caller thread for |
| 922 | * the specified maximum interval (in miliseconds). |
| 923 | */ |
| 924 | PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout) |
| 925 | { |
| 926 | unsigned count = 0; |
| 927 | pj_time_val tv; |
| 928 | pj_status_t status; |
| 929 | |
| 930 | tv.sec = 0; |
| 931 | tv.msec = msec_timeout; |
| 932 | pj_time_val_normalize(&tv); |
| 933 | |
| 934 | status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count); |
| 935 | |
| 936 | if (status != PJ_SUCCESS) |
| 937 | return -status; |
| 938 | |
| 939 | return count; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | /* |
| 944 | * Create memory pool. |
| 945 | */ |
| 946 | PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size, |
| 947 | pj_size_t increment) |
| 948 | { |
| 949 | /* Pool factory is thread safe, no need to lock */ |
| 950 | return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment, |
| 951 | NULL); |
| 952 | } |
| 953 | |
| 954 | |
| 955 | /* |
| 956 | * Internal function to get SIP endpoint instance of pjsua, which is |
| 957 | * needed for example to register module, create transports, etc. |
| 958 | * Probably is only valid after #pjsua_init() is called. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 959 | */ |
| 960 | PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void) |
| 961 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 962 | return pjsua_var.endpt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 965 | /* |
| 966 | * Internal function to get media endpoint instance. |
| 967 | * Only valid after #pjsua_init() is called. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 968 | */ |
| 969 | PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void) |
| 970 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 971 | return pjsua_var.med_endpt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Benny Prijono | 97b8717 | 2006-08-24 14:25:14 +0000 | [diff] [blame] | 974 | /* |
| 975 | * Internal function to get PJSUA pool factory. |
| 976 | */ |
| 977 | PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void) |
| 978 | { |
| 979 | return &pjsua_var.cp.factory; |
| 980 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 981 | |
| 982 | /***************************************************************************** |
| 983 | * PJSUA SIP Transport API. |
| 984 | */ |
| 985 | |
| 986 | /* |
| 987 | * Create and initialize SIP socket (and possibly resolve public |
| 988 | * address via STUN, depending on config). |
| 989 | */ |
| 990 | static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr, |
| 991 | int port, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 992 | pj_sock_t *p_sock, |
| 993 | pj_sockaddr_in *p_pub_addr) |
| 994 | { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 995 | char ip_addr[32]; |
| 996 | pj_str_t stun_srv; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 997 | pj_sock_t sock; |
| 998 | pj_status_t status; |
| 999 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1000 | /* Make sure STUN server resolution has completed */ |
| 1001 | status = pjsua_resolve_stun_server(PJ_TRUE); |
| 1002 | if (status != PJ_SUCCESS) { |
| 1003 | pjsua_perror(THIS_FILE, "Error resolving STUN server", status); |
| 1004 | return status; |
| 1005 | } |
| 1006 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1007 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock); |
| 1008 | if (status != PJ_SUCCESS) { |
| 1009 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1010 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Benny Prijono | f90ef4f | 2007-02-12 14:59:57 +0000 | [diff] [blame] | 1013 | status = pj_sock_bind_in(sock, pj_ntohl(bound_addr.s_addr), |
| 1014 | (pj_uint16_t)port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1015 | if (status != PJ_SUCCESS) { |
| 1016 | pjsua_perror(THIS_FILE, "bind() error", status); |
| 1017 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1018 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Benny Prijono | e347cb0 | 2007-02-14 14:36:13 +0000 | [diff] [blame] | 1021 | /* If port is zero, get the bound port */ |
| 1022 | if (port == 0) { |
| 1023 | pj_sockaddr_in bound_addr; |
| 1024 | int namelen = sizeof(bound_addr); |
| 1025 | status = pj_sock_getsockname(sock, &bound_addr, &namelen); |
| 1026 | if (status != PJ_SUCCESS) { |
| 1027 | pjsua_perror(THIS_FILE, "getsockname() error", status); |
| 1028 | pj_sock_close(sock); |
| 1029 | return status; |
| 1030 | } |
| 1031 | |
| 1032 | port = pj_ntohs(bound_addr.sin_port); |
| 1033 | } |
| 1034 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1035 | if (pjsua_var.stun_srv.addr.sa_family != 0) { |
| 1036 | pj_ansi_strcpy(ip_addr,pj_inet_ntoa(pjsua_var.stun_srv.ipv4.sin_addr)); |
| 1037 | stun_srv = pj_str(ip_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1038 | } else { |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1039 | stun_srv.slen = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* Get the published address, either by STUN or by resolving |
| 1043 | * the name of local host. |
| 1044 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1045 | if (stun_srv.slen) { |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1046 | /* |
| 1047 | * STUN is specified, resolve the address with STUN. |
| 1048 | */ |
Benny Prijono | 14c2b86 | 2007-02-21 00:40:05 +0000 | [diff] [blame] | 1049 | status = pjstun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1050 | &stun_srv, 3478, |
| 1051 | &stun_srv, 3478, |
| 1052 | p_pub_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1053 | if (status != PJ_SUCCESS) { |
Benny Prijono | ebbf689 | 2007-03-24 17:37:25 +0000 | [diff] [blame^] | 1054 | pjsua_perror(THIS_FILE, "Error contacting STUN server", status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1055 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1056 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1059 | } else if (p_pub_addr->sin_addr.s_addr != 0) { |
| 1060 | /* |
| 1061 | * Public address is already specified, no need to resolve the |
| 1062 | * address, only set the port. |
| 1063 | */ |
Benny Prijono | e347cb0 | 2007-02-14 14:36:13 +0000 | [diff] [blame] | 1064 | if (p_pub_addr->sin_port == 0) |
| 1065 | p_pub_addr->sin_port = pj_htons((pj_uint16_t)port); |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1066 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1067 | } else { |
| 1068 | |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 1069 | pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1070 | |
Benny Prijono | 594e4c5 | 2006-09-14 18:51:01 +0000 | [diff] [blame] | 1071 | status = pj_gethostip(&p_pub_addr->sin_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1072 | if (status != PJ_SUCCESS) { |
Benny Prijono | b43bad7 | 2007-01-20 05:11:08 +0000 | [diff] [blame] | 1073 | pjsua_perror(THIS_FILE, "Unable to get local host IP", status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1074 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1075 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1078 | p_pub_addr->sin_family = PJ_AF_INET; |
| 1079 | p_pub_addr->sin_port = pj_htons((pj_uint16_t)port); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | *p_sock = sock; |
| 1083 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1084 | PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d", |
| 1085 | pj_inet_ntoa(p_pub_addr->sin_addr), |
| 1086 | (int)pj_ntohs(p_pub_addr->sin_port))); |
| 1087 | |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1088 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /* |
| 1093 | * Create SIP transport. |
| 1094 | */ |
| 1095 | PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type, |
| 1096 | const pjsua_transport_config *cfg, |
| 1097 | pjsua_transport_id *p_id) |
| 1098 | { |
| 1099 | pjsip_transport *tp; |
| 1100 | unsigned id; |
| 1101 | pj_status_t status; |
| 1102 | |
| 1103 | PJSUA_LOCK(); |
| 1104 | |
| 1105 | /* Find empty transport slot */ |
| 1106 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1107 | if (pjsua_var.tpdata[id].data.ptr == NULL) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1108 | break; |
| 1109 | } |
| 1110 | |
| 1111 | if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 1112 | status = PJ_ETOOMANY; |
| 1113 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 1114 | goto on_return; |
| 1115 | } |
| 1116 | |
| 1117 | /* Create the transport */ |
| 1118 | if (type == PJSIP_TRANSPORT_UDP) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1119 | /* |
| 1120 | * Create UDP transport. |
| 1121 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1122 | pjsua_transport_config config; |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1123 | pj_sock_t sock = PJ_INVALID_SOCKET; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1124 | pj_sockaddr_in bound_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1125 | pj_sockaddr_in pub_addr; |
| 1126 | pjsip_host_port addr_name; |
| 1127 | |
| 1128 | /* Supply default config if it's not specified */ |
| 1129 | if (cfg == NULL) { |
| 1130 | pjsua_transport_config_default(&config); |
| 1131 | cfg = &config; |
| 1132 | } |
| 1133 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1134 | /* Initialize bound address, if any */ |
| 1135 | bound_addr.sin_addr.s_addr = PJ_INADDR_ANY; |
| 1136 | if (cfg->bound_addr.slen) { |
| 1137 | status = pj_sockaddr_in_set_str_addr(&bound_addr,&cfg->bound_addr); |
| 1138 | if (status != PJ_SUCCESS) { |
| 1139 | pjsua_perror(THIS_FILE, |
| 1140 | "Unable to resolve transport bound address", |
| 1141 | status); |
| 1142 | goto on_return; |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | /* Initialize the public address from the config, if any */ |
| 1147 | pj_sockaddr_in_init(&pub_addr, NULL, (pj_uint16_t)cfg->port); |
| 1148 | if (cfg->public_addr.slen) { |
| 1149 | status = pj_sockaddr_in_set_str_addr(&pub_addr, &cfg->public_addr); |
| 1150 | if (status != PJ_SUCCESS) { |
| 1151 | pjsua_perror(THIS_FILE, |
| 1152 | "Unable to resolve transport public address", |
| 1153 | status); |
| 1154 | goto on_return; |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /* Create the socket and possibly resolve the address with STUN |
| 1159 | * (only when public address is not specified). |
| 1160 | */ |
| 1161 | status = create_sip_udp_sock(bound_addr.sin_addr, cfg->port, |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1162 | &sock, &pub_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1163 | if (status != PJ_SUCCESS) |
| 1164 | goto on_return; |
| 1165 | |
| 1166 | addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr)); |
| 1167 | addr_name.port = pj_ntohs(pub_addr.sin_port); |
| 1168 | |
| 1169 | /* Create UDP transport */ |
| 1170 | status = pjsip_udp_transport_attach( pjsua_var.endpt, sock, |
| 1171 | &addr_name, 1, |
| 1172 | &tp); |
| 1173 | if (status != PJ_SUCCESS) { |
| 1174 | pjsua_perror(THIS_FILE, "Error creating SIP UDP transport", |
| 1175 | status); |
| 1176 | pj_sock_close(sock); |
| 1177 | goto on_return; |
| 1178 | } |
| 1179 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1180 | |
| 1181 | /* Save the transport */ |
| 1182 | pjsua_var.tpdata[id].type = type; |
| 1183 | pjsua_var.tpdata[id].local_name = tp->local_name; |
| 1184 | pjsua_var.tpdata[id].data.tp = tp; |
| 1185 | |
| 1186 | } else if (type == PJSIP_TRANSPORT_TCP) { |
| 1187 | /* |
| 1188 | * Create TCP transport. |
| 1189 | */ |
| 1190 | pjsua_transport_config config; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1191 | pjsip_host_port a_name; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1192 | pjsip_tpfactory *tcp; |
| 1193 | pj_sockaddr_in local_addr; |
| 1194 | |
| 1195 | /* Supply default config if it's not specified */ |
| 1196 | if (cfg == NULL) { |
| 1197 | pjsua_transport_config_default(&config); |
| 1198 | cfg = &config; |
| 1199 | } |
| 1200 | |
| 1201 | /* Init local address */ |
| 1202 | pj_sockaddr_in_init(&local_addr, 0, 0); |
| 1203 | |
| 1204 | if (cfg->port) |
| 1205 | local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port); |
| 1206 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1207 | if (cfg->bound_addr.slen) { |
| 1208 | status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr); |
| 1209 | if (status != PJ_SUCCESS) { |
| 1210 | pjsua_perror(THIS_FILE, |
| 1211 | "Unable to resolve transport bound address", |
| 1212 | status); |
| 1213 | goto on_return; |
| 1214 | } |
| 1215 | } |
| 1216 | |
| 1217 | /* Init published name */ |
| 1218 | pj_bzero(&a_name, sizeof(pjsip_host_port)); |
| 1219 | if (cfg->public_addr.slen) |
| 1220 | a_name.host = cfg->public_addr; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1221 | |
| 1222 | /* Create the TCP transport */ |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1223 | status = pjsip_tcp_transport_start2(pjsua_var.endpt, &local_addr, |
| 1224 | &a_name, 1, &tcp); |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1225 | |
| 1226 | if (status != PJ_SUCCESS) { |
| 1227 | pjsua_perror(THIS_FILE, "Error creating SIP TCP listener", |
| 1228 | status); |
| 1229 | goto on_return; |
| 1230 | } |
| 1231 | |
| 1232 | /* Save the transport */ |
| 1233 | pjsua_var.tpdata[id].type = type; |
| 1234 | pjsua_var.tpdata[id].local_name = tcp->addr_name; |
| 1235 | pjsua_var.tpdata[id].data.factory = tcp; |
| 1236 | |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1237 | #if defined(PJSIP_HAS_TLS_TRANSPORT) && PJSIP_HAS_TLS_TRANSPORT!=0 |
| 1238 | } else if (type == PJSIP_TRANSPORT_TLS) { |
| 1239 | /* |
| 1240 | * Create TLS transport. |
| 1241 | */ |
Benny Prijono | f3bbc13 | 2006-12-25 06:43:59 +0000 | [diff] [blame] | 1242 | /* |
| 1243 | * Create TCP transport. |
| 1244 | */ |
| 1245 | pjsua_transport_config config; |
| 1246 | pjsip_host_port a_name; |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1247 | pjsip_tpfactory *tls; |
Benny Prijono | f3bbc13 | 2006-12-25 06:43:59 +0000 | [diff] [blame] | 1248 | pj_sockaddr_in local_addr; |
| 1249 | |
| 1250 | /* Supply default config if it's not specified */ |
| 1251 | if (cfg == NULL) { |
| 1252 | pjsua_transport_config_default(&config); |
| 1253 | config.port = 5061; |
| 1254 | cfg = &config; |
| 1255 | } |
| 1256 | |
| 1257 | /* Init local address */ |
| 1258 | pj_sockaddr_in_init(&local_addr, 0, 0); |
| 1259 | |
| 1260 | if (cfg->port) |
| 1261 | local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port); |
| 1262 | |
| 1263 | if (cfg->bound_addr.slen) { |
| 1264 | status = pj_sockaddr_in_set_str_addr(&local_addr,&cfg->bound_addr); |
| 1265 | if (status != PJ_SUCCESS) { |
| 1266 | pjsua_perror(THIS_FILE, |
| 1267 | "Unable to resolve transport bound address", |
| 1268 | status); |
| 1269 | goto on_return; |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /* Init published name */ |
| 1274 | pj_bzero(&a_name, sizeof(pjsip_host_port)); |
| 1275 | if (cfg->public_addr.slen) |
| 1276 | a_name.host = cfg->public_addr; |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1277 | |
| 1278 | status = pjsip_tls_transport_start(pjsua_var.endpt, |
Benny Prijono | f3bbc13 | 2006-12-25 06:43:59 +0000 | [diff] [blame] | 1279 | &cfg->tls_setting, |
| 1280 | &local_addr, &a_name, 1, &tls); |
Benny Prijono | 6e0e54b | 2006-12-08 21:58:31 +0000 | [diff] [blame] | 1281 | if (status != PJ_SUCCESS) { |
| 1282 | pjsua_perror(THIS_FILE, "Error creating SIP TLS listener", |
| 1283 | status); |
| 1284 | goto on_return; |
| 1285 | } |
| 1286 | |
| 1287 | /* Save the transport */ |
| 1288 | pjsua_var.tpdata[id].type = type; |
| 1289 | pjsua_var.tpdata[id].local_name = tls->addr_name; |
| 1290 | pjsua_var.tpdata[id].data.factory = tls; |
| 1291 | #endif |
| 1292 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1293 | } else { |
| 1294 | status = PJSIP_EUNSUPTRANSPORT; |
| 1295 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 1296 | goto on_return; |
| 1297 | } |
| 1298 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1299 | |
| 1300 | /* Return the ID */ |
| 1301 | if (p_id) *p_id = id; |
| 1302 | |
| 1303 | status = PJ_SUCCESS; |
| 1304 | |
| 1305 | on_return: |
| 1306 | |
| 1307 | PJSUA_UNLOCK(); |
| 1308 | |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 1309 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | /* |
| 1314 | * Register transport that has been created by application. |
| 1315 | */ |
| 1316 | PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp, |
| 1317 | pjsua_transport_id *p_id) |
| 1318 | { |
| 1319 | unsigned id; |
| 1320 | |
| 1321 | PJSUA_LOCK(); |
| 1322 | |
| 1323 | /* Find empty transport slot */ |
| 1324 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1325 | if (pjsua_var.tpdata[id].data.ptr == NULL) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | } |
| 1328 | |
| 1329 | if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 1330 | pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY); |
| 1331 | PJSUA_UNLOCK(); |
| 1332 | return PJ_ETOOMANY; |
| 1333 | } |
| 1334 | |
| 1335 | /* Save the transport */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1336 | pjsua_var.tpdata[id].type = tp->key.type; |
| 1337 | pjsua_var.tpdata[id].local_name = tp->local_name; |
| 1338 | pjsua_var.tpdata[id].data.tp = tp; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1339 | |
| 1340 | /* Return the ID */ |
| 1341 | if (p_id) *p_id = id; |
| 1342 | |
| 1343 | PJSUA_UNLOCK(); |
| 1344 | |
| 1345 | return PJ_SUCCESS; |
| 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | /* |
| 1350 | * Enumerate all transports currently created in the system. |
| 1351 | */ |
| 1352 | PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[], |
| 1353 | unsigned *p_count ) |
| 1354 | { |
| 1355 | unsigned i, count; |
| 1356 | |
| 1357 | PJSUA_LOCK(); |
| 1358 | |
| 1359 | for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count; |
| 1360 | ++i) |
| 1361 | { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1362 | if (!pjsua_var.tpdata[i].data.ptr) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1363 | continue; |
| 1364 | |
| 1365 | id[count++] = i; |
| 1366 | } |
| 1367 | |
| 1368 | *p_count = count; |
| 1369 | |
| 1370 | PJSUA_UNLOCK(); |
| 1371 | |
| 1372 | return PJ_SUCCESS; |
| 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | /* |
| 1377 | * Get information about transports. |
| 1378 | */ |
| 1379 | PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id, |
| 1380 | pjsua_transport_info *info) |
| 1381 | { |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1382 | pjsua_transport_data *t = &pjsua_var.tpdata[id]; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1383 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1384 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1385 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1386 | |
| 1387 | /* Make sure id is in range. */ |
| 1388 | PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL); |
| 1389 | |
| 1390 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1391 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1392 | |
| 1393 | PJSUA_LOCK(); |
| 1394 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1395 | if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) { |
| 1396 | |
| 1397 | pjsip_transport *tp = t->data.tp; |
| 1398 | |
| 1399 | if (tp == NULL) { |
| 1400 | PJSUA_UNLOCK(); |
| 1401 | return PJ_EINVALIDOP; |
| 1402 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1403 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1404 | info->id = id; |
| 1405 | info->type = tp->key.type; |
| 1406 | info->type_name = pj_str(tp->type_name); |
| 1407 | info->info = pj_str(tp->info); |
| 1408 | info->flag = tp->flag; |
| 1409 | info->addr_len = tp->addr_len; |
| 1410 | info->local_addr = tp->local_addr; |
| 1411 | info->local_name = tp->local_name; |
| 1412 | info->usage_count = pj_atomic_get(tp->ref_cnt); |
| 1413 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1414 | status = PJ_SUCCESS; |
| 1415 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1416 | } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) { |
| 1417 | |
| 1418 | pjsip_tpfactory *factory = t->data.factory; |
| 1419 | |
| 1420 | if (factory == NULL) { |
| 1421 | PJSUA_UNLOCK(); |
| 1422 | return PJ_EINVALIDOP; |
| 1423 | } |
| 1424 | |
| 1425 | info->id = id; |
| 1426 | info->type = t->type; |
| 1427 | info->type_name = pj_str("TCP"); |
| 1428 | info->info = pj_str("TCP transport"); |
| 1429 | info->flag = factory->flag; |
| 1430 | info->addr_len = sizeof(factory->local_addr); |
| 1431 | info->local_addr = factory->local_addr; |
| 1432 | info->local_name = factory->addr_name; |
| 1433 | info->usage_count = 0; |
| 1434 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1435 | status = PJ_SUCCESS; |
| 1436 | |
| 1437 | } else { |
| 1438 | pj_assert(!"Unsupported transport"); |
| 1439 | status = PJ_EINVALIDOP; |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1442 | |
| 1443 | PJSUA_UNLOCK(); |
| 1444 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1445 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | /* |
| 1450 | * Disable a transport or re-enable it. |
| 1451 | */ |
| 1452 | PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id, |
| 1453 | pj_bool_t enabled) |
| 1454 | { |
| 1455 | /* Make sure id is in range. */ |
| 1456 | PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL); |
| 1457 | |
| 1458 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1459 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1460 | |
| 1461 | |
| 1462 | /* To be done!! */ |
| 1463 | PJ_TODO(pjsua_transport_set_enable); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1464 | PJ_UNUSED_ARG(enabled); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1465 | |
| 1466 | return PJ_EINVALIDOP; |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | /* |
| 1471 | * Close the transport. |
| 1472 | */ |
| 1473 | PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id, |
| 1474 | pj_bool_t force ) |
| 1475 | { |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1476 | pj_status_t status; |
| 1477 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1478 | /* Make sure id is in range. */ |
| 1479 | PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL); |
| 1480 | |
| 1481 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1482 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1483 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1484 | /* Note: destroy() may not work if there are objects still referencing |
| 1485 | * the transport. |
| 1486 | */ |
| 1487 | if (force) { |
| 1488 | switch (pjsua_var.tpdata[id].type) { |
| 1489 | case PJSIP_TRANSPORT_UDP: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1490 | status = pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp); |
| 1491 | if (status != PJ_SUCCESS) |
| 1492 | return status; |
| 1493 | status = pjsip_transport_destroy(pjsua_var.tpdata[id].data.tp); |
| 1494 | if (status != PJ_SUCCESS) |
| 1495 | return status; |
| 1496 | break; |
| 1497 | |
| 1498 | case PJSIP_TRANSPORT_TLS: |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1499 | case PJSIP_TRANSPORT_TCP: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1500 | /* This will close the TCP listener, but existing TCP/TLS |
| 1501 | * connections (if any) will still linger |
| 1502 | */ |
| 1503 | status = (*pjsua_var.tpdata[id].data.factory->destroy) |
| 1504 | (pjsua_var.tpdata[id].data.factory); |
| 1505 | if (status != PJ_SUCCESS) |
| 1506 | return status; |
| 1507 | |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1508 | break; |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1509 | |
Benny Prijono | 1ebd614 | 2006-10-19 15:48:02 +0000 | [diff] [blame] | 1510 | default: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1511 | return PJ_EINVAL; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | } else { |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1515 | /* If force is not specified, transports will be closed at their |
| 1516 | * convenient time. However this will leak PJSUA-API transport |
| 1517 | * descriptors as PJSUA-API wouldn't know when exactly the |
| 1518 | * transport is closed thus it can't cleanup PJSUA transport |
| 1519 | * descriptor. |
| 1520 | */ |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1521 | switch (pjsua_var.tpdata[id].type) { |
| 1522 | case PJSIP_TRANSPORT_UDP: |
| 1523 | return pjsip_transport_shutdown(pjsua_var.tpdata[id].data.tp); |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1524 | case PJSIP_TRANSPORT_TLS: |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1525 | case PJSIP_TRANSPORT_TCP: |
| 1526 | return (*pjsua_var.tpdata[id].data.factory->destroy) |
| 1527 | (pjsua_var.tpdata[id].data.factory); |
Benny Prijono | 1ebd614 | 2006-10-19 15:48:02 +0000 | [diff] [blame] | 1528 | default: |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1529 | return PJ_EINVAL; |
Benny Prijono | 0a5cad8 | 2006-09-26 13:21:02 +0000 | [diff] [blame] | 1530 | } |
| 1531 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1532 | |
Benny Prijono | 5ff6187 | 2007-02-01 03:37:11 +0000 | [diff] [blame] | 1533 | /* Cleanup pjsua data when force is applied */ |
| 1534 | if (force) { |
| 1535 | pjsua_var.tpdata[id].type = PJSIP_TRANSPORT_UNSPECIFIED; |
| 1536 | pjsua_var.tpdata[id].data.ptr = NULL; |
| 1537 | } |
| 1538 | |
| 1539 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | /* |
| 1544 | * Add additional headers etc in msg_data specified by application |
| 1545 | * when sending requests. |
| 1546 | */ |
| 1547 | void pjsua_process_msg_data(pjsip_tx_data *tdata, |
| 1548 | const pjsua_msg_data *msg_data) |
| 1549 | { |
| 1550 | pj_bool_t allow_body; |
| 1551 | const pjsip_hdr *hdr; |
| 1552 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1553 | /* Always add User-Agent */ |
| 1554 | if (pjsua_var.ua_cfg.user_agent.slen && |
| 1555 | tdata->msg->type == PJSIP_REQUEST_MSG) |
| 1556 | { |
| 1557 | const pj_str_t STR_USER_AGENT = { "User-Agent", 10 }; |
| 1558 | pjsip_hdr *h; |
| 1559 | h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, |
| 1560 | &STR_USER_AGENT, |
| 1561 | &pjsua_var.ua_cfg.user_agent); |
| 1562 | pjsip_msg_add_hdr(tdata->msg, h); |
| 1563 | } |
| 1564 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1565 | if (!msg_data) |
| 1566 | return; |
| 1567 | |
| 1568 | hdr = msg_data->hdr_list.next; |
| 1569 | while (hdr && hdr != &msg_data->hdr_list) { |
| 1570 | pjsip_hdr *new_hdr; |
| 1571 | |
| 1572 | new_hdr = pjsip_hdr_clone(tdata->pool, hdr); |
| 1573 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 1574 | |
| 1575 | hdr = hdr->next; |
| 1576 | } |
| 1577 | |
| 1578 | allow_body = (tdata->msg->body == NULL); |
| 1579 | |
| 1580 | if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) { |
| 1581 | pjsip_media_type ctype; |
| 1582 | pjsip_msg_body *body; |
| 1583 | |
| 1584 | pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype); |
| 1585 | body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype, |
| 1586 | &msg_data->msg_body); |
| 1587 | tdata->msg->body = body; |
| 1588 | } |
| 1589 | } |
| 1590 | |
| 1591 | |
| 1592 | /* |
| 1593 | * Add route_set to outgoing requests |
| 1594 | */ |
| 1595 | void pjsua_set_msg_route_set( pjsip_tx_data *tdata, |
| 1596 | const pjsip_route_hdr *route_set ) |
| 1597 | { |
| 1598 | const pjsip_route_hdr *r; |
| 1599 | |
| 1600 | r = route_set->next; |
| 1601 | while (r != route_set) { |
| 1602 | pjsip_route_hdr *new_r; |
| 1603 | |
| 1604 | new_r = pjsip_hdr_clone(tdata->pool, r); |
| 1605 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r); |
| 1606 | |
| 1607 | r = r->next; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | |
| 1612 | /* |
| 1613 | * Simple version of MIME type parsing (it doesn't support parameters) |
| 1614 | */ |
| 1615 | void pjsua_parse_media_type( pj_pool_t *pool, |
| 1616 | const pj_str_t *mime, |
| 1617 | pjsip_media_type *media_type) |
| 1618 | { |
| 1619 | pj_str_t tmp; |
| 1620 | char *pos; |
| 1621 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1622 | pj_bzero(media_type, sizeof(*media_type)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1623 | |
| 1624 | pj_strdup_with_null(pool, &tmp, mime); |
| 1625 | |
| 1626 | pos = pj_strchr(&tmp, '/'); |
| 1627 | if (pos) { |
| 1628 | media_type->type.ptr = tmp.ptr; |
| 1629 | media_type->type.slen = (pos-tmp.ptr); |
| 1630 | media_type->subtype.ptr = pos+1; |
| 1631 | media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1; |
| 1632 | } else { |
| 1633 | media_type->type = tmp; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | /* |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 1639 | * Internal function to init transport selector from transport id. |
| 1640 | */ |
| 1641 | void pjsua_init_tpselector(pjsua_transport_id tp_id, |
| 1642 | pjsip_tpselector *sel) |
| 1643 | { |
| 1644 | pjsua_transport_data *tpdata; |
| 1645 | unsigned flag; |
| 1646 | |
| 1647 | pj_bzero(sel, sizeof(*sel)); |
| 1648 | if (tp_id == PJSUA_INVALID_ID) |
| 1649 | return; |
| 1650 | |
| 1651 | pj_assert(tp_id >= 0 && tp_id < PJ_ARRAY_SIZE(pjsua_var.tpdata)); |
| 1652 | tpdata = &pjsua_var.tpdata[tp_id]; |
| 1653 | |
| 1654 | flag = pjsip_transport_get_flag_from_type(tpdata->type); |
| 1655 | |
| 1656 | if (flag & PJSIP_TRANSPORT_DATAGRAM) { |
| 1657 | sel->type = PJSIP_TPSELECTOR_TRANSPORT; |
| 1658 | sel->u.transport = tpdata->data.tp; |
| 1659 | } else { |
| 1660 | sel->type = PJSIP_TPSELECTOR_LISTENER; |
| 1661 | sel->u.listener = tpdata->data.factory; |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | |
| 1666 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1667 | * Verify that valid SIP url is given. |
| 1668 | */ |
| 1669 | PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url) |
| 1670 | { |
| 1671 | pjsip_uri *p; |
| 1672 | pj_pool_t *pool; |
| 1673 | char *url; |
| 1674 | int len = (c_url ? pj_ansi_strlen(c_url) : 0); |
| 1675 | |
| 1676 | if (!len) return -1; |
| 1677 | |
| 1678 | pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL); |
| 1679 | if (!pool) return -1; |
| 1680 | |
| 1681 | url = pj_pool_alloc(pool, len+1); |
| 1682 | pj_ansi_strcpy(url, c_url); |
| 1683 | |
| 1684 | p = pjsip_parse_uri(pool, url, len, 0); |
| 1685 | if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0) |
| 1686 | p = NULL; |
| 1687 | |
| 1688 | pj_pool_release(pool); |
| 1689 | return p ? 0 : -1; |
| 1690 | } |