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