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