Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org> |
| 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 | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 288 | /* Write to stdout, file, and application callback. */ |
| 289 | |
| 290 | if (level <= (int)pjsua_var.log_cfg.console_level) |
| 291 | pj_log_write(level, buffer, len); |
| 292 | |
| 293 | if (pjsua_var.log_file) { |
| 294 | pj_ssize_t size = len; |
| 295 | pj_file_write(pjsua_var.log_file, buffer, &size); |
| 296 | } |
| 297 | |
| 298 | if (pjsua_var.log_cfg.cb) |
| 299 | (*pjsua_var.log_cfg.cb)(level, buffer, len); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /* |
| 304 | * Application can call this function at any time (after pjsua_create(), of |
| 305 | * course) to change logging settings. |
| 306 | */ |
| 307 | PJ_DEF(pj_status_t) pjsua_reconfigure_logging(const pjsua_logging_config *cfg) |
| 308 | { |
| 309 | pj_status_t status; |
| 310 | |
| 311 | /* Save config. */ |
| 312 | pjsua_logging_config_dup(pjsua_var.pool, &pjsua_var.log_cfg, cfg); |
| 313 | |
| 314 | /* Redirect log function to ours */ |
| 315 | pj_log_set_log_func( &log_writer ); |
| 316 | |
| 317 | /* Close existing file, if any */ |
| 318 | if (pjsua_var.log_file) { |
| 319 | pj_file_close(pjsua_var.log_file); |
| 320 | pjsua_var.log_file = NULL; |
| 321 | } |
| 322 | |
| 323 | /* If output log file is desired, create the file: */ |
| 324 | if (pjsua_var.log_cfg.log_filename.slen) { |
| 325 | |
| 326 | status = pj_file_open(pjsua_var.pool, |
| 327 | pjsua_var.log_cfg.log_filename.ptr, |
| 328 | PJ_O_WRONLY, |
| 329 | &pjsua_var.log_file); |
| 330 | |
| 331 | if (status != PJ_SUCCESS) { |
| 332 | pjsua_perror(THIS_FILE, "Error creating log file", status); |
| 333 | return status; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /* Unregister msg logging if it's previously registered */ |
| 338 | if (pjsua_msg_logger.id >= 0) { |
| 339 | pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 340 | pjsua_msg_logger.id = -1; |
| 341 | } |
| 342 | |
| 343 | /* Enable SIP message logging */ |
| 344 | if (pjsua_var.log_cfg.msg_logging) |
| 345 | pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger); |
| 346 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 347 | return PJ_SUCCESS; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | /***************************************************************************** |
| 352 | * PJSUA Base API. |
| 353 | */ |
| 354 | |
| 355 | /* Worker thread function. */ |
| 356 | static int worker_thread(void *arg) |
| 357 | { |
| 358 | enum { TIMEOUT = 10 }; |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 359 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 360 | PJ_UNUSED_ARG(arg); |
| 361 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 362 | while (!pjsua_var.thread_quit_flag) { |
| 363 | int count; |
| 364 | |
| 365 | count = pjsua_handle_events(TIMEOUT); |
| 366 | if (count < 0) |
| 367 | pj_thread_sleep(TIMEOUT); |
| 368 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 373 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 374 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 375 | * Instantiate pjsua application. |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 376 | */ |
| 377 | PJ_DEF(pj_status_t) pjsua_create(void) |
| 378 | { |
| 379 | pj_status_t status; |
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 | /* Init pjsua data */ |
| 382 | init_data(); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 383 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 384 | /* Set default logging settings */ |
| 385 | pjsua_logging_config_default(&pjsua_var.log_cfg); |
| 386 | |
| 387 | /* Init PJLIB: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 388 | status = pj_init(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 389 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 390 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 391 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 392 | /* Init PJLIB-UTIL: */ |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 393 | status = pjlib_util_init(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 394 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 395 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 396 | |
| 397 | /* Init caching pool. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 398 | 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] | 399 | |
| 400 | /* Create memory pool for application. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 401 | pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000); |
| 402 | |
| 403 | PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 404 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 405 | /* Create mutex */ |
| 406 | status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua", |
| 407 | &pjsua_var.mutex); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 408 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 409 | pjsua_perror(THIS_FILE, "Unable to create mutex", status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 410 | return status; |
| 411 | } |
| 412 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 413 | /* Must create SIP endpoint to initialize SIP parser. The parser |
| 414 | * is needed for example when application needs to call pjsua_verify_url(). |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 415 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 416 | status = pjsip_endpt_create(&pjsua_var.cp.factory, |
| 417 | pj_gethostname()->ptr, |
| 418 | &pjsua_var.endpt); |
| 419 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 420 | |
| 421 | |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 422 | return PJ_SUCCESS; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 427 | * Initialize pjsua with the specified settings. All the settings are |
| 428 | * optional, and the default values will be used when the config is not |
| 429 | * specified. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 430 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 431 | PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg, |
| 432 | const pjsua_logging_config *log_cfg, |
| 433 | const pjsua_media_config *media_cfg) |
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 | pjsua_config default_cfg; |
| 436 | pjsua_media_config default_media_cfg; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 437 | const pj_str_t STR_OPTIONS = { "OPTIONS", 7 }; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 438 | pj_status_t status; |
| 439 | |
| 440 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 441 | /* Create default configurations when the config is not supplied */ |
| 442 | |
| 443 | if (ua_cfg == NULL) { |
| 444 | pjsua_config_default(&default_cfg); |
| 445 | ua_cfg = &default_cfg; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 448 | if (media_cfg == NULL) { |
| 449 | pjsua_media_config_default(&default_media_cfg); |
| 450 | media_cfg = &default_media_cfg; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 453 | /* Initialize logging first so that info/errors can be captured */ |
| 454 | if (log_cfg) { |
| 455 | status = pjsua_reconfigure_logging(log_cfg); |
| 456 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 459 | /* Init SIP UA: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 460 | |
| 461 | /* Initialize transaction layer: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 462 | status = pjsip_tsx_layer_init_module(pjsua_var.endpt); |
| 463 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 464 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 465 | |
| 466 | /* Initialize UA layer module: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 467 | status = pjsip_ua_init_module( pjsua_var.endpt, NULL ); |
| 468 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 469 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 470 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 471 | /* Initialize and register PJSUA application module. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 472 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 473 | const pjsip_module mod_initializer = |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 474 | { |
| 475 | NULL, NULL, /* prev, next. */ |
| 476 | { "mod-pjsua", 9 }, /* Name. */ |
| 477 | -1, /* Id */ |
| 478 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 479 | NULL, /* load() */ |
| 480 | NULL, /* start() */ |
| 481 | NULL, /* stop() */ |
| 482 | NULL, /* unload() */ |
| 483 | &mod_pjsua_on_rx_request, /* on_rx_request() */ |
| 484 | &mod_pjsua_on_rx_response, /* on_rx_response() */ |
| 485 | NULL, /* on_tx_request. */ |
| 486 | NULL, /* on_tx_response() */ |
| 487 | NULL, /* on_tsx_state() */ |
| 488 | }; |
| 489 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 490 | pjsua_var.mod = mod_initializer; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 491 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 492 | status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod); |
| 493 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 496 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 497 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 498 | /* Initialize PJSUA call subsystem: */ |
| 499 | status = pjsua_call_subsys_init(ua_cfg); |
| 500 | if (status != PJ_SUCCESS) |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 501 | goto on_error; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 502 | |
| 503 | |
| 504 | /* Initialize PJSUA media subsystem */ |
| 505 | status = pjsua_media_subsys_init(media_cfg); |
| 506 | if (status != PJ_SUCCESS) |
| 507 | goto on_error; |
| 508 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 509 | |
| 510 | /* Init core SIMPLE module : */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 511 | status = pjsip_evsub_init_module(pjsua_var.endpt); |
| 512 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 513 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 514 | |
| 515 | /* Init presence module: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 516 | status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance()); |
| 517 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 518 | |
Benny Prijono | 3a5e1ab | 2006-08-15 20:26:34 +0000 | [diff] [blame] | 519 | /* Init PUBLISH module */ |
| 520 | pjsip_publishc_init_module(pjsua_var.endpt); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 521 | |
| 522 | /* Init xfer/REFER module */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 523 | status = pjsip_xfer_init_module( pjsua_var.endpt ); |
| 524 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 525 | |
| 526 | /* Init pjsua presence handler: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 527 | status = pjsua_pres_init(); |
| 528 | if (status != PJ_SUCCESS) |
| 529 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 530 | |
| 531 | /* Init out-of-dialog MESSAGE request handler. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 532 | status = pjsua_im_init(); |
| 533 | if (status != PJ_SUCCESS) |
| 534 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 535 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 536 | /* Register OPTIONS handler */ |
| 537 | pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler); |
| 538 | |
| 539 | /* Add OPTIONS in Allow header */ |
| 540 | pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_ALLOW, |
| 541 | NULL, 1, &STR_OPTIONS); |
| 542 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 543 | /* Start worker thread if needed. */ |
| 544 | if (pjsua_var.ua_cfg.thread_cnt) { |
| 545 | unsigned i; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 546 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 547 | if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread)) |
| 548 | pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 549 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 550 | for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 551 | status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread, |
| 552 | NULL, 0, 0, &pjsua_var.thread[i]); |
| 553 | if (status != PJ_SUCCESS) |
| 554 | goto on_error; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 555 | } |
Benny Prijono | f521eb0 | 2006-08-06 23:07:25 +0000 | [diff] [blame] | 556 | PJ_LOG(4,(THIS_FILE, "%d SIP worker threads created", |
| 557 | pjsua_var.ua_cfg.thread_cnt)); |
| 558 | } else { |
| 559 | PJ_LOG(4,(THIS_FILE, "No SIP worker threads created")); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 562 | /* Done! */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 563 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 564 | PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized", |
| 565 | PJ_VERSION, PJ_OS_NAME)); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 566 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 567 | return PJ_SUCCESS; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 568 | |
| 569 | on_error: |
| 570 | pjsua_destroy(); |
| 571 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 575 | /* Sleep with polling */ |
| 576 | static void busy_sleep(unsigned msec) |
| 577 | { |
| 578 | pj_time_val timeout, now; |
| 579 | |
| 580 | pj_gettimeofday(&timeout); |
| 581 | timeout.msec += msec; |
| 582 | pj_time_val_normalize(&timeout); |
| 583 | |
| 584 | do { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 585 | while (pjsua_handle_events(10) > 0) |
| 586 | ; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 587 | pj_gettimeofday(&now); |
| 588 | } while (PJ_TIME_VAL_LT(now, timeout)); |
| 589 | } |
| 590 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 591 | /* |
| 592 | * Destroy pjsua. |
| 593 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 594 | PJ_DEF(pj_status_t) pjsua_destroy(void) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 595 | { |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 596 | int i; /* Must be signed */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 597 | |
| 598 | /* Signal threads to quit: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 599 | pjsua_var.thread_quit_flag = 1; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 600 | |
| 601 | /* Wait worker threads to quit: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 602 | for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) { |
| 603 | if (pjsua_var.thread[i]) { |
| 604 | pj_thread_join(pjsua_var.thread[i]); |
| 605 | pj_thread_destroy(pjsua_var.thread[i]); |
| 606 | pjsua_var.thread[i] = NULL; |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 607 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 608 | } |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 609 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 610 | if (pjsua_var.endpt) { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 611 | /* Terminate all calls. */ |
| 612 | pjsua_call_hangup_all(); |
| 613 | |
| 614 | /* Terminate all presence subscriptions. */ |
| 615 | pjsua_pres_shutdown(); |
| 616 | |
| 617 | /* Unregister, if required: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 618 | for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) { |
| 619 | if (!pjsua_var.acc[i].valid) |
| 620 | continue; |
| 621 | |
| 622 | if (pjsua_var.acc[i].regc) { |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 623 | pjsua_acc_set_registration(i, PJ_FALSE); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | /* Wait for some time to allow unregistration to complete: */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 628 | PJ_LOG(4,(THIS_FILE, "Shutting down...")); |
| 629 | busy_sleep(1000); |
| 630 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 631 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 632 | /* Destroy media */ |
| 633 | pjsua_media_subsys_destroy(); |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 634 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 635 | /* Destroy endpoint. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 636 | if (pjsua_var.endpt) { |
| 637 | pjsip_endpt_destroy(pjsua_var.endpt); |
| 638 | pjsua_var.endpt = NULL; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 639 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 640 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 641 | /* Destroy mutex */ |
| 642 | if (pjsua_var.mutex) { |
| 643 | pj_mutex_destroy(pjsua_var.mutex); |
| 644 | pjsua_var.mutex = NULL; |
| 645 | } |
| 646 | |
| 647 | /* Destroy pool and pool factory. */ |
| 648 | if (pjsua_var.pool) { |
| 649 | pj_pool_release(pjsua_var.pool); |
| 650 | pjsua_var.pool = NULL; |
| 651 | pj_caching_pool_destroy(&pjsua_var.cp); |
| 652 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 653 | |
| 654 | |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 655 | PJ_LOG(4,(THIS_FILE, "PJSUA destroyed...")); |
| 656 | |
| 657 | /* End logging */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 658 | if (pjsua_var.log_file) { |
| 659 | pj_file_close(pjsua_var.log_file); |
| 660 | pjsua_var.log_file = NULL; |
| 661 | } |
Benny Prijono | b9b32ab | 2006-06-01 12:28:44 +0000 | [diff] [blame] | 662 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 663 | /* Done. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 664 | return PJ_SUCCESS; |
| 665 | } |
| 666 | |
| 667 | |
| 668 | /** |
| 669 | * Application is recommended to call this function after all initialization |
| 670 | * is done, so that the library can do additional checking set up |
| 671 | * additional |
| 672 | * |
| 673 | * @return PJ_SUCCESS on success, or the appropriate error code. |
| 674 | */ |
| 675 | PJ_DEF(pj_status_t) pjsua_start(void) |
| 676 | { |
| 677 | pj_status_t status; |
| 678 | |
| 679 | status = pjsua_call_subsys_start(); |
| 680 | if (status != PJ_SUCCESS) |
| 681 | return status; |
| 682 | |
| 683 | status = pjsua_media_subsys_start(); |
| 684 | if (status != PJ_SUCCESS) |
| 685 | return status; |
| 686 | |
| 687 | status = pjsua_pres_start(); |
| 688 | if (status != PJ_SUCCESS) |
| 689 | return status; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 690 | |
| 691 | return PJ_SUCCESS; |
| 692 | } |
| 693 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 694 | |
| 695 | /** |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 696 | * Poll pjsua for events, and if necessary block the caller thread for |
| 697 | * the specified maximum interval (in miliseconds). |
| 698 | */ |
| 699 | PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout) |
| 700 | { |
| 701 | unsigned count = 0; |
| 702 | pj_time_val tv; |
| 703 | pj_status_t status; |
| 704 | |
| 705 | tv.sec = 0; |
| 706 | tv.msec = msec_timeout; |
| 707 | pj_time_val_normalize(&tv); |
| 708 | |
| 709 | status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count); |
| 710 | |
| 711 | if (status != PJ_SUCCESS) |
| 712 | return -status; |
| 713 | |
| 714 | return count; |
| 715 | } |
| 716 | |
| 717 | |
| 718 | /* |
| 719 | * Create memory pool. |
| 720 | */ |
| 721 | PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size, |
| 722 | pj_size_t increment) |
| 723 | { |
| 724 | /* Pool factory is thread safe, no need to lock */ |
| 725 | return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment, |
| 726 | NULL); |
| 727 | } |
| 728 | |
| 729 | |
| 730 | /* |
| 731 | * Internal function to get SIP endpoint instance of pjsua, which is |
| 732 | * needed for example to register module, create transports, etc. |
| 733 | * Probably is only valid after #pjsua_init() is called. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 734 | */ |
| 735 | PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void) |
| 736 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 737 | return pjsua_var.endpt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 740 | /* |
| 741 | * Internal function to get media endpoint instance. |
| 742 | * Only valid after #pjsua_init() is called. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 743 | */ |
| 744 | PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void) |
| 745 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 746 | return pjsua_var.med_endpt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Benny Prijono | 97b8717 | 2006-08-24 14:25:14 +0000 | [diff] [blame^] | 749 | /* |
| 750 | * Internal function to get PJSUA pool factory. |
| 751 | */ |
| 752 | PJ_DEF(pj_pool_factory*) pjsua_get_pool_factory(void) |
| 753 | { |
| 754 | return &pjsua_var.cp.factory; |
| 755 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 756 | |
| 757 | /***************************************************************************** |
| 758 | * PJSUA SIP Transport API. |
| 759 | */ |
| 760 | |
| 761 | /* |
| 762 | * Create and initialize SIP socket (and possibly resolve public |
| 763 | * address via STUN, depending on config). |
| 764 | */ |
| 765 | static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr, |
| 766 | int port, |
| 767 | pj_bool_t use_stun, |
| 768 | const pjsua_stun_config *stun_param, |
| 769 | pj_sock_t *p_sock, |
| 770 | pj_sockaddr_in *p_pub_addr) |
| 771 | { |
| 772 | pjsua_stun_config stun; |
| 773 | pj_sock_t sock; |
| 774 | pj_status_t status; |
| 775 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 776 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock); |
| 777 | if (status != PJ_SUCCESS) { |
| 778 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 779 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | status = pj_sock_bind_in(sock, bound_addr.s_addr, (pj_uint16_t)port); |
| 783 | if (status != PJ_SUCCESS) { |
| 784 | pjsua_perror(THIS_FILE, "bind() error", status); |
| 785 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 786 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* Copy and normalize STUN param */ |
| 790 | if (use_stun) { |
| 791 | pj_memcpy(&stun, stun_param, sizeof(*stun_param)); |
| 792 | pjsua_normalize_stun_config(&stun); |
| 793 | } else { |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 794 | pj_bzero(&stun, sizeof(pjsua_stun_config)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /* Get the published address, either by STUN or by resolving |
| 798 | * the name of local host. |
| 799 | */ |
| 800 | if (stun.stun_srv1.slen) { |
| 801 | status = pj_stun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock, |
| 802 | &stun.stun_srv1, |
| 803 | stun.stun_port1, |
| 804 | &stun.stun_srv2, |
| 805 | stun.stun_port2, |
| 806 | p_pub_addr); |
| 807 | if (status != PJ_SUCCESS) { |
| 808 | pjsua_perror(THIS_FILE, "Error resolving with STUN", status); |
| 809 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 810 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | } else { |
| 814 | |
| 815 | const pj_str_t *hostname = pj_gethostname(); |
| 816 | struct pj_hostent he; |
| 817 | |
| 818 | status = pj_gethostbyname(hostname, &he); |
| 819 | if (status != PJ_SUCCESS) { |
| 820 | pjsua_perror(THIS_FILE, "Unable to resolve local host", status); |
| 821 | pj_sock_close(sock); |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 822 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 825 | pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 826 | p_pub_addr->sin_family = PJ_AF_INET; |
| 827 | p_pub_addr->sin_port = pj_htons((pj_uint16_t)port); |
| 828 | p_pub_addr->sin_addr = *(pj_in_addr*)he.h_addr; |
| 829 | } |
| 830 | |
| 831 | *p_sock = sock; |
| 832 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 833 | PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d", |
| 834 | pj_inet_ntoa(p_pub_addr->sin_addr), |
| 835 | (int)pj_ntohs(p_pub_addr->sin_port))); |
| 836 | |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 837 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | |
| 841 | /* |
| 842 | * Create SIP transport. |
| 843 | */ |
| 844 | PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type, |
| 845 | const pjsua_transport_config *cfg, |
| 846 | pjsua_transport_id *p_id) |
| 847 | { |
| 848 | pjsip_transport *tp; |
| 849 | unsigned id; |
| 850 | pj_status_t status; |
| 851 | |
| 852 | PJSUA_LOCK(); |
| 853 | |
| 854 | /* Find empty transport slot */ |
| 855 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 856 | if (pjsua_var.tpdata[id].data.ptr == NULL) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 857 | break; |
| 858 | } |
| 859 | |
| 860 | if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 861 | status = PJ_ETOOMANY; |
| 862 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 863 | goto on_return; |
| 864 | } |
| 865 | |
| 866 | /* Create the transport */ |
| 867 | if (type == PJSIP_TRANSPORT_UDP) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 868 | /* |
| 869 | * Create UDP transport. |
| 870 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 871 | pjsua_transport_config config; |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 872 | pj_sock_t sock = PJ_INVALID_SOCKET; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 873 | pj_sockaddr_in pub_addr; |
| 874 | pjsip_host_port addr_name; |
| 875 | |
| 876 | /* Supply default config if it's not specified */ |
| 877 | if (cfg == NULL) { |
| 878 | pjsua_transport_config_default(&config); |
| 879 | cfg = &config; |
| 880 | } |
| 881 | |
| 882 | /* Create the socket and possibly resolve the address with STUN */ |
| 883 | status = create_sip_udp_sock(cfg->ip_addr, cfg->port, cfg->use_stun, |
| 884 | &cfg->stun_config, &sock, &pub_addr); |
| 885 | if (status != PJ_SUCCESS) |
| 886 | goto on_return; |
| 887 | |
| 888 | addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr)); |
| 889 | addr_name.port = pj_ntohs(pub_addr.sin_port); |
| 890 | |
| 891 | /* Create UDP transport */ |
| 892 | status = pjsip_udp_transport_attach( pjsua_var.endpt, sock, |
| 893 | &addr_name, 1, |
| 894 | &tp); |
| 895 | if (status != PJ_SUCCESS) { |
| 896 | pjsua_perror(THIS_FILE, "Error creating SIP UDP transport", |
| 897 | status); |
| 898 | pj_sock_close(sock); |
| 899 | goto on_return; |
| 900 | } |
| 901 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 902 | |
| 903 | /* Save the transport */ |
| 904 | pjsua_var.tpdata[id].type = type; |
| 905 | pjsua_var.tpdata[id].local_name = tp->local_name; |
| 906 | pjsua_var.tpdata[id].data.tp = tp; |
| 907 | |
| 908 | } else if (type == PJSIP_TRANSPORT_TCP) { |
| 909 | /* |
| 910 | * Create TCP transport. |
| 911 | */ |
| 912 | pjsua_transport_config config; |
| 913 | pjsip_tpfactory *tcp; |
| 914 | pj_sockaddr_in local_addr; |
| 915 | |
| 916 | /* Supply default config if it's not specified */ |
| 917 | if (cfg == NULL) { |
| 918 | pjsua_transport_config_default(&config); |
| 919 | cfg = &config; |
| 920 | } |
| 921 | |
| 922 | /* Init local address */ |
| 923 | pj_sockaddr_in_init(&local_addr, 0, 0); |
| 924 | |
| 925 | if (cfg->port) |
| 926 | local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port); |
| 927 | |
| 928 | if (cfg->ip_addr.s_addr) |
| 929 | local_addr.sin_addr.s_addr = cfg->ip_addr.s_addr; |
| 930 | |
| 931 | /* Create the TCP transport */ |
| 932 | status = pjsip_tcp_transport_start(pjsua_var.endpt, &local_addr, 1, |
| 933 | &tcp); |
| 934 | |
| 935 | if (status != PJ_SUCCESS) { |
| 936 | pjsua_perror(THIS_FILE, "Error creating SIP TCP listener", |
| 937 | status); |
| 938 | goto on_return; |
| 939 | } |
| 940 | |
| 941 | /* Save the transport */ |
| 942 | pjsua_var.tpdata[id].type = type; |
| 943 | pjsua_var.tpdata[id].local_name = tcp->addr_name; |
| 944 | pjsua_var.tpdata[id].data.factory = tcp; |
| 945 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 946 | } else { |
| 947 | status = PJSIP_EUNSUPTRANSPORT; |
| 948 | pjsua_perror(THIS_FILE, "Error creating transport", status); |
| 949 | goto on_return; |
| 950 | } |
| 951 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 952 | |
| 953 | /* Return the ID */ |
| 954 | if (p_id) *p_id = id; |
| 955 | |
| 956 | status = PJ_SUCCESS; |
| 957 | |
| 958 | on_return: |
| 959 | |
| 960 | PJSUA_UNLOCK(); |
| 961 | |
Benny Prijono | d841053 | 2006-06-15 11:04:33 +0000 | [diff] [blame] | 962 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | |
| 966 | /* |
| 967 | * Register transport that has been created by application. |
| 968 | */ |
| 969 | PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp, |
| 970 | pjsua_transport_id *p_id) |
| 971 | { |
| 972 | unsigned id; |
| 973 | |
| 974 | PJSUA_LOCK(); |
| 975 | |
| 976 | /* Find empty transport slot */ |
| 977 | for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 978 | if (pjsua_var.tpdata[id].data.ptr == NULL) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 979 | break; |
| 980 | } |
| 981 | |
| 982 | if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) { |
| 983 | pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY); |
| 984 | PJSUA_UNLOCK(); |
| 985 | return PJ_ETOOMANY; |
| 986 | } |
| 987 | |
| 988 | /* Save the transport */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 989 | pjsua_var.tpdata[id].type = tp->key.type; |
| 990 | pjsua_var.tpdata[id].local_name = tp->local_name; |
| 991 | pjsua_var.tpdata[id].data.tp = tp; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 992 | |
| 993 | /* Return the ID */ |
| 994 | if (p_id) *p_id = id; |
| 995 | |
| 996 | PJSUA_UNLOCK(); |
| 997 | |
| 998 | return PJ_SUCCESS; |
| 999 | } |
| 1000 | |
| 1001 | |
| 1002 | /* |
| 1003 | * Enumerate all transports currently created in the system. |
| 1004 | */ |
| 1005 | PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[], |
| 1006 | unsigned *p_count ) |
| 1007 | { |
| 1008 | unsigned i, count; |
| 1009 | |
| 1010 | PJSUA_LOCK(); |
| 1011 | |
| 1012 | for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count; |
| 1013 | ++i) |
| 1014 | { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1015 | if (!pjsua_var.tpdata[i].data.ptr) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1016 | continue; |
| 1017 | |
| 1018 | id[count++] = i; |
| 1019 | } |
| 1020 | |
| 1021 | *p_count = count; |
| 1022 | |
| 1023 | PJSUA_UNLOCK(); |
| 1024 | |
| 1025 | return PJ_SUCCESS; |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | /* |
| 1030 | * Get information about transports. |
| 1031 | */ |
| 1032 | PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id, |
| 1033 | pjsua_transport_info *info) |
| 1034 | { |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1035 | struct transport_data *t = &pjsua_var.tpdata[id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1036 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1037 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1038 | |
| 1039 | /* Make sure id is in range. */ |
| 1040 | PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL); |
| 1041 | |
| 1042 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1043 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1044 | |
| 1045 | PJSUA_LOCK(); |
| 1046 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1047 | if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) { |
| 1048 | |
| 1049 | pjsip_transport *tp = t->data.tp; |
| 1050 | |
| 1051 | if (tp == NULL) { |
| 1052 | PJSUA_UNLOCK(); |
| 1053 | return PJ_EINVALIDOP; |
| 1054 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1055 | |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1056 | info->id = id; |
| 1057 | info->type = tp->key.type; |
| 1058 | info->type_name = pj_str(tp->type_name); |
| 1059 | info->info = pj_str(tp->info); |
| 1060 | info->flag = tp->flag; |
| 1061 | info->addr_len = tp->addr_len; |
| 1062 | info->local_addr = tp->local_addr; |
| 1063 | info->local_name = tp->local_name; |
| 1064 | info->usage_count = pj_atomic_get(tp->ref_cnt); |
| 1065 | |
| 1066 | } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) { |
| 1067 | |
| 1068 | pjsip_tpfactory *factory = t->data.factory; |
| 1069 | |
| 1070 | if (factory == NULL) { |
| 1071 | PJSUA_UNLOCK(); |
| 1072 | return PJ_EINVALIDOP; |
| 1073 | } |
| 1074 | |
| 1075 | info->id = id; |
| 1076 | info->type = t->type; |
| 1077 | info->type_name = pj_str("TCP"); |
| 1078 | info->info = pj_str("TCP transport"); |
| 1079 | info->flag = factory->flag; |
| 1080 | info->addr_len = sizeof(factory->local_addr); |
| 1081 | info->local_addr = factory->local_addr; |
| 1082 | info->local_name = factory->addr_name; |
| 1083 | info->usage_count = 0; |
| 1084 | |
| 1085 | } |
| 1086 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1087 | |
| 1088 | PJSUA_UNLOCK(); |
| 1089 | |
| 1090 | return PJ_EINVALIDOP; |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | /* |
| 1095 | * Disable a transport or re-enable it. |
| 1096 | */ |
| 1097 | PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id, |
| 1098 | pj_bool_t enabled) |
| 1099 | { |
| 1100 | /* Make sure id is in range. */ |
| 1101 | PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL); |
| 1102 | |
| 1103 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1104 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1105 | |
| 1106 | |
| 1107 | /* To be done!! */ |
| 1108 | PJ_TODO(pjsua_transport_set_enable); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1109 | PJ_UNUSED_ARG(enabled); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1110 | |
| 1111 | return PJ_EINVALIDOP; |
| 1112 | } |
| 1113 | |
| 1114 | |
| 1115 | /* |
| 1116 | * Close the transport. |
| 1117 | */ |
| 1118 | PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id, |
| 1119 | pj_bool_t force ) |
| 1120 | { |
| 1121 | /* Make sure id is in range. */ |
| 1122 | PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL); |
| 1123 | |
| 1124 | /* Make sure that transport exists */ |
Benny Prijono | e93e287 | 2006-06-28 16:46:49 +0000 | [diff] [blame] | 1125 | PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1126 | |
| 1127 | |
| 1128 | /* To be done!! */ |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1129 | PJ_UNUSED_ARG(force); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1130 | |
| 1131 | PJ_TODO(pjsua_transport_close); |
| 1132 | |
| 1133 | return PJ_EINVALIDOP; |
| 1134 | } |
| 1135 | |
| 1136 | |
| 1137 | /* |
| 1138 | * Add additional headers etc in msg_data specified by application |
| 1139 | * when sending requests. |
| 1140 | */ |
| 1141 | void pjsua_process_msg_data(pjsip_tx_data *tdata, |
| 1142 | const pjsua_msg_data *msg_data) |
| 1143 | { |
| 1144 | pj_bool_t allow_body; |
| 1145 | const pjsip_hdr *hdr; |
| 1146 | |
Benny Prijono | 5631561 | 2006-07-18 14:39:40 +0000 | [diff] [blame] | 1147 | /* Always add User-Agent */ |
| 1148 | if (pjsua_var.ua_cfg.user_agent.slen && |
| 1149 | tdata->msg->type == PJSIP_REQUEST_MSG) |
| 1150 | { |
| 1151 | const pj_str_t STR_USER_AGENT = { "User-Agent", 10 }; |
| 1152 | pjsip_hdr *h; |
| 1153 | h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool, |
| 1154 | &STR_USER_AGENT, |
| 1155 | &pjsua_var.ua_cfg.user_agent); |
| 1156 | pjsip_msg_add_hdr(tdata->msg, h); |
| 1157 | } |
| 1158 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1159 | if (!msg_data) |
| 1160 | return; |
| 1161 | |
| 1162 | hdr = msg_data->hdr_list.next; |
| 1163 | while (hdr && hdr != &msg_data->hdr_list) { |
| 1164 | pjsip_hdr *new_hdr; |
| 1165 | |
| 1166 | new_hdr = pjsip_hdr_clone(tdata->pool, hdr); |
| 1167 | pjsip_msg_add_hdr(tdata->msg, new_hdr); |
| 1168 | |
| 1169 | hdr = hdr->next; |
| 1170 | } |
| 1171 | |
| 1172 | allow_body = (tdata->msg->body == NULL); |
| 1173 | |
| 1174 | if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) { |
| 1175 | pjsip_media_type ctype; |
| 1176 | pjsip_msg_body *body; |
| 1177 | |
| 1178 | pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype); |
| 1179 | body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype, |
| 1180 | &msg_data->msg_body); |
| 1181 | tdata->msg->body = body; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | |
| 1186 | /* |
| 1187 | * Add route_set to outgoing requests |
| 1188 | */ |
| 1189 | void pjsua_set_msg_route_set( pjsip_tx_data *tdata, |
| 1190 | const pjsip_route_hdr *route_set ) |
| 1191 | { |
| 1192 | const pjsip_route_hdr *r; |
| 1193 | |
| 1194 | r = route_set->next; |
| 1195 | while (r != route_set) { |
| 1196 | pjsip_route_hdr *new_r; |
| 1197 | |
| 1198 | new_r = pjsip_hdr_clone(tdata->pool, r); |
| 1199 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r); |
| 1200 | |
| 1201 | r = r->next; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | /* |
| 1207 | * Simple version of MIME type parsing (it doesn't support parameters) |
| 1208 | */ |
| 1209 | void pjsua_parse_media_type( pj_pool_t *pool, |
| 1210 | const pj_str_t *mime, |
| 1211 | pjsip_media_type *media_type) |
| 1212 | { |
| 1213 | pj_str_t tmp; |
| 1214 | char *pos; |
| 1215 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1216 | pj_bzero(media_type, sizeof(*media_type)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1217 | |
| 1218 | pj_strdup_with_null(pool, &tmp, mime); |
| 1219 | |
| 1220 | pos = pj_strchr(&tmp, '/'); |
| 1221 | if (pos) { |
| 1222 | media_type->type.ptr = tmp.ptr; |
| 1223 | media_type->type.slen = (pos-tmp.ptr); |
| 1224 | media_type->subtype.ptr = pos+1; |
| 1225 | media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1; |
| 1226 | } else { |
| 1227 | media_type->type = tmp; |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | |
| 1232 | /* |
| 1233 | * Verify that valid SIP url is given. |
| 1234 | */ |
| 1235 | PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url) |
| 1236 | { |
| 1237 | pjsip_uri *p; |
| 1238 | pj_pool_t *pool; |
| 1239 | char *url; |
| 1240 | int len = (c_url ? pj_ansi_strlen(c_url) : 0); |
| 1241 | |
| 1242 | if (!len) return -1; |
| 1243 | |
| 1244 | pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL); |
| 1245 | if (!pool) return -1; |
| 1246 | |
| 1247 | url = pj_pool_alloc(pool, len+1); |
| 1248 | pj_ansi_strcpy(url, c_url); |
| 1249 | |
| 1250 | p = pjsip_parse_uri(pool, url, len, 0); |
| 1251 | if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0) |
| 1252 | p = NULL; |
| 1253 | |
| 1254 | pj_pool_release(pool); |
| 1255 | return p ? 0 : -1; |
| 1256 | } |