blob: faafe2bde8d39baa9d6fd32faae9272e8a82b57c [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $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 Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijonoeebe9af2006-06-13 22:57:13 +000020#include <pjsua-lib/pjsua_internal.h>
Benny Prijono268ca612006-02-07 12:34:11 +000021
Benny Prijono268ca612006-02-07 12:34:11 +000022
Benny Prijono84126ab2006-02-09 09:30:09 +000023#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000024
25
Benny Prijonoeebe9af2006-06-13 22:57:13 +000026/* PJSUA application instance. */
27struct pjsua_data pjsua_var;
Benny Prijono84126ab2006-02-09 09:30:09 +000028
29
Benny Prijonoeebe9af2006-06-13 22:57:13 +000030/* Display error */
31PJ_DEF(void) pjsua_perror( const char *sender, const char *title,
32 pj_status_t status)
Benny Prijono268ca612006-02-07 12:34:11 +000033{
Benny Prijonoeebe9af2006-06-13 22:57:13 +000034 char errmsg[PJ_ERR_MSG_SIZE];
Benny Prijonoa91a0032006-02-26 21:23:45 +000035
Benny Prijonoeebe9af2006-06-13 22:57:13 +000036 pj_strerror(status, errmsg, sizeof(errmsg));
37 PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status));
Benny Prijono268ca612006-02-07 12:34:11 +000038}
39
40
Benny Prijonoeebe9af2006-06-13 22:57:13 +000041static void init_data()
Benny Prijonodc39fe82006-05-26 12:17:46 +000042{
43 unsigned i;
44
Benny Prijonoeebe9af2006-06-13 22:57:13 +000045 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 Prijonodc39fe82006-05-26 12:17:46 +000051
52
Benny Prijonodc39fe82006-05-26 12:17:46 +000053
Benny Prijonoeebe9af2006-06-13 22:57:13 +000054/*****************************************************************************
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 Prijonodc39fe82006-05-26 12:17:46 +000067
Benny Prijonoeebe9af2006-06-13 22:57:13 +000068/* Notification on incoming messages */
69static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
70{
Benny Prijonob4a17c92006-07-10 14:40:21 +000071 PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
72 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000073 "--end msg--",
74 rdata->msg_info.len,
75 pjsip_rx_data_get_info(rdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +000076 rdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000077 rdata->pkt_info.src_name,
78 rdata->pkt_info.src_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +000079 (int)rdata->msg_info.len,
Benny Prijonoeebe9af2006-06-13 22:57:13 +000080 rdata->msg_info.msg_buf));
81
82 /* Always return false, otherwise messages will not get processed! */
83 return PJ_FALSE;
84}
Benny Prijonodc39fe82006-05-26 12:17:46 +000085
Benny Prijonoeebe9af2006-06-13 22:57:13 +000086/* Notification on outgoing messages */
87static 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 Prijonodc39fe82006-05-26 12:17:46 +000095
Benny Prijonob4a17c92006-07-10 14:40:21 +000096 PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s %s:%d:\n"
97 "%.*s\n"
Benny Prijonoeebe9af2006-06-13 22:57:13 +000098 "--end msg--",
99 (tdata->buf.cur - tdata->buf.start),
100 pjsip_tx_data_get_info(tdata),
Benny Prijonob4a17c92006-07-10 14:40:21 +0000101 tdata->tp_info.transport->type_name,
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000102 tdata->tp_info.dst_name,
103 tdata->tp_info.dst_port,
Benny Prijonob4a17c92006-07-10 14:40:21 +0000104 (int)(tdata->buf.cur - tdata->buf.start),
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000105 tdata->buf.start));
Benny Prijonodc39fe82006-05-26 12:17:46 +0000106
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000107 /* Always return success, otherwise message will not get sent! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000108 return PJ_SUCCESS;
109}
110
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000111/* The module instance. */
112static 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 Prijono56315612006-07-18 14:39:40 +0000132 * Another simple module to handle incoming OPTIONS request
133 */
134
135/* Notification on incoming request */
136static 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. */
212static 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 Prijonoeebe9af2006-06-13 22:57:13 +0000232 * 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 Prijono268ca612006-02-07 12:34:11 +0000236
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 */
246static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
247{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000248 pj_bool_t processed = PJ_FALSE;
249
250 PJSUA_LOCK();
Benny Prijono38998232006-02-08 22:44:25 +0000251
Benny Prijono84126ab2006-02-09 09:30:09 +0000252 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000253
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000254 processed = pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000255 }
256
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000257 PJSUA_UNLOCK();
258
259 return processed;
Benny Prijono268ca612006-02-07 12:34:11 +0000260}
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 */
274static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
275{
276 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000277 return PJ_FALSE;
278}
279
280
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000281/*****************************************************************************
282 * Logging.
283 */
284
285/* Log callback */
286static void log_writer(int level, const char *buffer, int len)
Benny Prijono268ca612006-02-07 12:34:11 +0000287{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000288 /* 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 */
307PJ_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
Benny Prijono56315612006-07-18 14:39:40 +0000337 /* Unregister OPTIONS handler if it's previously registered */
338 if (pjsua_options_handler.id >= 0) {
339 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_options_handler);
340 pjsua_options_handler.id = -1;
341 }
342
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000343 /* Unregister msg logging if it's previously registered */
344 if (pjsua_msg_logger.id >= 0) {
345 pjsip_endpt_unregister_module(pjsua_var.endpt, &pjsua_msg_logger);
346 pjsua_msg_logger.id = -1;
347 }
348
349 /* Enable SIP message logging */
350 if (pjsua_var.log_cfg.msg_logging)
351 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_msg_logger);
352
Benny Prijono56315612006-07-18 14:39:40 +0000353 /* Register OPTIONS handler */
354 pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_options_handler);
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000355
356 return PJ_SUCCESS;
357}
358
359
360/*****************************************************************************
361 * PJSUA Base API.
362 */
363
364/* Worker thread function. */
365static int worker_thread(void *arg)
366{
367 enum { TIMEOUT = 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000368
Benny Prijono268ca612006-02-07 12:34:11 +0000369 PJ_UNUSED_ARG(arg);
370
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000371 while (!pjsua_var.thread_quit_flag) {
372 int count;
373
374 count = pjsua_handle_events(TIMEOUT);
375 if (count < 0)
376 pj_thread_sleep(TIMEOUT);
377 }
Benny Prijono268ca612006-02-07 12:34:11 +0000378
379 return 0;
380}
381
Benny Prijonodc39fe82006-05-26 12:17:46 +0000382
Benny Prijono268ca612006-02-07 12:34:11 +0000383/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000384 * Instantiate pjsua application.
Benny Prijonodc39fe82006-05-26 12:17:46 +0000385 */
386PJ_DEF(pj_status_t) pjsua_create(void)
387{
388 pj_status_t status;
Benny Prijono268ca612006-02-07 12:34:11 +0000389
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000390 /* Init pjsua data */
391 init_data();
Benny Prijono268ca612006-02-07 12:34:11 +0000392
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000393 /* Set default logging settings */
394 pjsua_logging_config_default(&pjsua_var.log_cfg);
395
396 /* Init PJLIB: */
Benny Prijono268ca612006-02-07 12:34:11 +0000397 status = pj_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000398 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
399
Benny Prijono268ca612006-02-07 12:34:11 +0000400
Benny Prijonofccab712006-02-22 22:23:22 +0000401 /* Init PJLIB-UTIL: */
Benny Prijonofccab712006-02-22 22:23:22 +0000402 status = pjlib_util_init();
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000403 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonofccab712006-02-22 22:23:22 +0000404
Benny Prijono268ca612006-02-07 12:34:11 +0000405
406 /* Init caching pool. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000407 pj_caching_pool_init(&pjsua_var.cp, &pj_pool_factory_default_policy, 0);
Benny Prijono268ca612006-02-07 12:34:11 +0000408
409 /* Create memory pool for application. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000410 pjsua_var.pool = pjsua_pool_create("pjsua", 4000, 4000);
411
412 PJ_ASSERT_RETURN(pjsua_var.pool, PJ_ENOMEM);
Benny Prijono268ca612006-02-07 12:34:11 +0000413
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000414 /* Create mutex */
415 status = pj_mutex_create_recursive(pjsua_var.pool, "pjsua",
416 &pjsua_var.mutex);
Benny Prijonoccf95622006-02-07 18:48:01 +0000417 if (status != PJ_SUCCESS) {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000418 pjsua_perror(THIS_FILE, "Unable to create mutex", status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000419 return status;
420 }
421
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000422 /* Must create SIP endpoint to initialize SIP parser. The parser
423 * is needed for example when application needs to call pjsua_verify_url().
Benny Prijonob8c25182006-04-29 08:31:09 +0000424 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000425 status = pjsip_endpt_create(&pjsua_var.cp.factory,
426 pj_gethostname()->ptr,
427 &pjsua_var.endpt);
428 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono39879152006-02-23 02:09:10 +0000429
430
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000431 return PJ_SUCCESS;
432}
433
434
435/*
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000436 * Initialize pjsua with the specified settings. All the settings are
437 * optional, and the default values will be used when the config is not
438 * specified.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000439 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000440PJ_DEF(pj_status_t) pjsua_init( const pjsua_config *ua_cfg,
441 const pjsua_logging_config *log_cfg,
442 const pjsua_media_config *media_cfg)
Benny Prijono9fc735d2006-05-28 14:58:12 +0000443{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000444 pjsua_config default_cfg;
445 pjsua_media_config default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000446 pj_status_t status;
447
448
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000449 /* Create default configurations when the config is not supplied */
450
451 if (ua_cfg == NULL) {
452 pjsua_config_default(&default_cfg);
453 ua_cfg = &default_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000454 }
455
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000456 if (media_cfg == NULL) {
457 pjsua_media_config_default(&default_media_cfg);
458 media_cfg = &default_media_cfg;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000459 }
460
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000461 /* Initialize logging first so that info/errors can be captured */
462 if (log_cfg) {
463 status = pjsua_reconfigure_logging(log_cfg);
464 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono9fc735d2006-05-28 14:58:12 +0000465 }
466
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000467 /* Init SIP UA: */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000468
469 /* Initialize transaction layer: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000470 status = pjsip_tsx_layer_init_module(pjsua_var.endpt);
471 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000472
Benny Prijonodc39fe82006-05-26 12:17:46 +0000473
474 /* Initialize UA layer module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000475 status = pjsip_ua_init_module( pjsua_var.endpt, NULL );
476 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000477
Benny Prijonodc39fe82006-05-26 12:17:46 +0000478
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000479 /* Initialize and register PJSUA application module. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000480 {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000481 const pjsip_module mod_initializer =
Benny Prijonodc39fe82006-05-26 12:17:46 +0000482 {
483 NULL, NULL, /* prev, next. */
484 { "mod-pjsua", 9 }, /* Name. */
485 -1, /* Id */
486 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
487 NULL, /* load() */
488 NULL, /* start() */
489 NULL, /* stop() */
490 NULL, /* unload() */
491 &mod_pjsua_on_rx_request, /* on_rx_request() */
492 &mod_pjsua_on_rx_response, /* on_rx_response() */
493 NULL, /* on_tx_request. */
494 NULL, /* on_tx_response() */
495 NULL, /* on_tsx_state() */
496 };
497
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000498 pjsua_var.mod = mod_initializer;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000499
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000500 status = pjsip_endpt_register_module(pjsua_var.endpt, &pjsua_var.mod);
501 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000502 }
503
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000504
Benny Prijonodc39fe82006-05-26 12:17:46 +0000505
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000506 /* Initialize PJSUA call subsystem: */
507 status = pjsua_call_subsys_init(ua_cfg);
508 if (status != PJ_SUCCESS)
Benny Prijonodc39fe82006-05-26 12:17:46 +0000509 goto on_error;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000510
511
512 /* Initialize PJSUA media subsystem */
513 status = pjsua_media_subsys_init(media_cfg);
514 if (status != PJ_SUCCESS)
515 goto on_error;
516
Benny Prijonodc39fe82006-05-26 12:17:46 +0000517
518 /* Init core SIMPLE module : */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000519 status = pjsip_evsub_init_module(pjsua_var.endpt);
520 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000521
Benny Prijonodc39fe82006-05-26 12:17:46 +0000522
523 /* Init presence module: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000524 status = pjsip_pres_init_module( pjsua_var.endpt, pjsip_evsub_instance());
525 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000526
Benny Prijonodc39fe82006-05-26 12:17:46 +0000527
528 /* Init xfer/REFER module */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000529 status = pjsip_xfer_init_module( pjsua_var.endpt );
530 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000531
532 /* Init pjsua presence handler: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000533 status = pjsua_pres_init();
534 if (status != PJ_SUCCESS)
535 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000536
537 /* Init out-of-dialog MESSAGE request handler. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000538 status = pjsua_im_init();
539 if (status != PJ_SUCCESS)
540 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000541
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000542 /* Start worker thread if needed. */
543 if (pjsua_var.ua_cfg.thread_cnt) {
544 unsigned i;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000545
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000546 if (pjsua_var.ua_cfg.thread_cnt > PJ_ARRAY_SIZE(pjsua_var.thread))
547 pjsua_var.ua_cfg.thread_cnt = PJ_ARRAY_SIZE(pjsua_var.thread);
Benny Prijonodc39fe82006-05-26 12:17:46 +0000548
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000549 for (i=0; i<pjsua_var.ua_cfg.thread_cnt; ++i) {
550 status = pj_thread_create(pjsua_var.pool, "pjsua", &worker_thread,
551 NULL, 0, 0, &pjsua_var.thread[i]);
552 if (status != PJ_SUCCESS)
553 goto on_error;
Benny Prijonodc39fe82006-05-26 12:17:46 +0000554 }
555 }
556
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000557 /* Done! */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000558
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000559 PJ_LOG(3,(THIS_FILE, "pjsua version %s for %s initialized",
560 PJ_VERSION, PJ_OS_NAME));
Benny Prijonoccf95622006-02-07 18:48:01 +0000561
Benny Prijono268ca612006-02-07 12:34:11 +0000562 return PJ_SUCCESS;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000563
564on_error:
565 pjsua_destroy();
566 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000567}
568
569
Benny Prijono834aee32006-02-19 01:38:06 +0000570/* Sleep with polling */
571static void busy_sleep(unsigned msec)
572{
573 pj_time_val timeout, now;
574
575 pj_gettimeofday(&timeout);
576 timeout.msec += msec;
577 pj_time_val_normalize(&timeout);
578
579 do {
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000580 while (pjsua_handle_events(10) > 0)
581 ;
Benny Prijono834aee32006-02-19 01:38:06 +0000582 pj_gettimeofday(&now);
583 } while (PJ_TIME_VAL_LT(now, timeout));
584}
585
Benny Prijono268ca612006-02-07 12:34:11 +0000586/*
587 * Destroy pjsua.
588 */
Benny Prijonodc39fe82006-05-26 12:17:46 +0000589PJ_DEF(pj_status_t) pjsua_destroy(void)
Benny Prijono268ca612006-02-07 12:34:11 +0000590{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000591 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000592
593 /* Signal threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000594 pjsua_var.thread_quit_flag = 1;
Benny Prijono268ca612006-02-07 12:34:11 +0000595
596 /* Wait worker threads to quit: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000597 for (i=0; i<(int)pjsua_var.ua_cfg.thread_cnt; ++i) {
598 if (pjsua_var.thread[i]) {
599 pj_thread_join(pjsua_var.thread[i]);
600 pj_thread_destroy(pjsua_var.thread[i]);
601 pjsua_var.thread[i] = NULL;
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000602 }
Benny Prijono268ca612006-02-07 12:34:11 +0000603 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000604
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000605 if (pjsua_var.endpt) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000606 /* Terminate all calls. */
607 pjsua_call_hangup_all();
608
609 /* Terminate all presence subscriptions. */
610 pjsua_pres_shutdown();
611
612 /* Unregister, if required: */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000613 for (i=0; i<(int)PJ_ARRAY_SIZE(pjsua_var.acc); ++i) {
614 if (!pjsua_var.acc[i].valid)
615 continue;
616
617 if (pjsua_var.acc[i].regc) {
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000618 pjsua_acc_set_registration(i, PJ_FALSE);
619 }
620 }
621
622 /* Wait for some time to allow unregistration to complete: */
Benny Prijono9fc735d2006-05-28 14:58:12 +0000623 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
624 busy_sleep(1000);
625 }
Benny Prijono834aee32006-02-19 01:38:06 +0000626
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000627 /* Destroy media */
628 pjsua_media_subsys_destroy();
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000629
Benny Prijono268ca612006-02-07 12:34:11 +0000630 /* Destroy endpoint. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000631 if (pjsua_var.endpt) {
632 pjsip_endpt_destroy(pjsua_var.endpt);
633 pjsua_var.endpt = NULL;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000634 }
Benny Prijono268ca612006-02-07 12:34:11 +0000635
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000636 /* Destroy mutex */
637 if (pjsua_var.mutex) {
638 pj_mutex_destroy(pjsua_var.mutex);
639 pjsua_var.mutex = NULL;
640 }
641
642 /* Destroy pool and pool factory. */
643 if (pjsua_var.pool) {
644 pj_pool_release(pjsua_var.pool);
645 pjsua_var.pool = NULL;
646 pj_caching_pool_destroy(&pjsua_var.cp);
647 }
Benny Prijono268ca612006-02-07 12:34:11 +0000648
649
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000650 PJ_LOG(4,(THIS_FILE, "PJSUA destroyed..."));
651
652 /* End logging */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000653 if (pjsua_var.log_file) {
654 pj_file_close(pjsua_var.log_file);
655 pjsua_var.log_file = NULL;
656 }
Benny Prijonob9b32ab2006-06-01 12:28:44 +0000657
Benny Prijono268ca612006-02-07 12:34:11 +0000658 /* Done. */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000659 return PJ_SUCCESS;
660}
661
662
663/**
664 * Application is recommended to call this function after all initialization
665 * is done, so that the library can do additional checking set up
666 * additional
667 *
668 * @return PJ_SUCCESS on success, or the appropriate error code.
669 */
670PJ_DEF(pj_status_t) pjsua_start(void)
671{
672 pj_status_t status;
673
674 status = pjsua_call_subsys_start();
675 if (status != PJ_SUCCESS)
676 return status;
677
678 status = pjsua_media_subsys_start();
679 if (status != PJ_SUCCESS)
680 return status;
681
682 status = pjsua_pres_start();
683 if (status != PJ_SUCCESS)
684 return status;
Benny Prijono268ca612006-02-07 12:34:11 +0000685
686 return PJ_SUCCESS;
687}
688
Benny Prijono9fc735d2006-05-28 14:58:12 +0000689
690/**
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000691 * Poll pjsua for events, and if necessary block the caller thread for
692 * the specified maximum interval (in miliseconds).
693 */
694PJ_DEF(int) pjsua_handle_events(unsigned msec_timeout)
695{
696 unsigned count = 0;
697 pj_time_val tv;
698 pj_status_t status;
699
700 tv.sec = 0;
701 tv.msec = msec_timeout;
702 pj_time_val_normalize(&tv);
703
704 status = pjsip_endpt_handle_events2(pjsua_var.endpt, &tv, &count);
705
706 if (status != PJ_SUCCESS)
707 return -status;
708
709 return count;
710}
711
712
713/*
714 * Create memory pool.
715 */
716PJ_DEF(pj_pool_t*) pjsua_pool_create( const char *name, pj_size_t init_size,
717 pj_size_t increment)
718{
719 /* Pool factory is thread safe, no need to lock */
720 return pj_pool_create(&pjsua_var.cp.factory, name, init_size, increment,
721 NULL);
722}
723
724
725/*
726 * Internal function to get SIP endpoint instance of pjsua, which is
727 * needed for example to register module, create transports, etc.
728 * Probably is only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000729 */
730PJ_DEF(pjsip_endpoint*) pjsua_get_pjsip_endpt(void)
731{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000732 return pjsua_var.endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000733}
734
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000735/*
736 * Internal function to get media endpoint instance.
737 * Only valid after #pjsua_init() is called.
Benny Prijono9fc735d2006-05-28 14:58:12 +0000738 */
739PJ_DEF(pjmedia_endpt*) pjsua_get_pjmedia_endpt(void)
740{
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000741 return pjsua_var.med_endpt;
Benny Prijono9fc735d2006-05-28 14:58:12 +0000742}
743
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000744
745/*****************************************************************************
746 * PJSUA SIP Transport API.
747 */
748
749/*
750 * Create and initialize SIP socket (and possibly resolve public
751 * address via STUN, depending on config).
752 */
753static pj_status_t create_sip_udp_sock(pj_in_addr bound_addr,
754 int port,
755 pj_bool_t use_stun,
756 const pjsua_stun_config *stun_param,
757 pj_sock_t *p_sock,
758 pj_sockaddr_in *p_pub_addr)
759{
760 pjsua_stun_config stun;
761 pj_sock_t sock;
762 pj_status_t status;
763
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000764 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock);
765 if (status != PJ_SUCCESS) {
766 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijonod8410532006-06-15 11:04:33 +0000767 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000768 }
769
770 status = pj_sock_bind_in(sock, bound_addr.s_addr, (pj_uint16_t)port);
771 if (status != PJ_SUCCESS) {
772 pjsua_perror(THIS_FILE, "bind() error", status);
773 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000774 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000775 }
776
777 /* Copy and normalize STUN param */
778 if (use_stun) {
779 pj_memcpy(&stun, stun_param, sizeof(*stun_param));
780 pjsua_normalize_stun_config(&stun);
781 } else {
Benny Prijonoac623b32006-07-03 15:19:31 +0000782 pj_bzero(&stun, sizeof(pjsua_stun_config));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000783 }
784
785 /* Get the published address, either by STUN or by resolving
786 * the name of local host.
787 */
788 if (stun.stun_srv1.slen) {
789 status = pj_stun_get_mapped_addr(&pjsua_var.cp.factory, 1, &sock,
790 &stun.stun_srv1,
791 stun.stun_port1,
792 &stun.stun_srv2,
793 stun.stun_port2,
794 p_pub_addr);
795 if (status != PJ_SUCCESS) {
796 pjsua_perror(THIS_FILE, "Error resolving with STUN", status);
797 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000798 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000799 }
800
801 } else {
802
803 const pj_str_t *hostname = pj_gethostname();
804 struct pj_hostent he;
805
806 status = pj_gethostbyname(hostname, &he);
807 if (status != PJ_SUCCESS) {
808 pjsua_perror(THIS_FILE, "Unable to resolve local host", status);
809 pj_sock_close(sock);
Benny Prijonod8410532006-06-15 11:04:33 +0000810 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000811 }
812
Benny Prijonoac623b32006-07-03 15:19:31 +0000813 pj_bzero(p_pub_addr, sizeof(pj_sockaddr_in));
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000814 p_pub_addr->sin_family = PJ_AF_INET;
815 p_pub_addr->sin_port = pj_htons((pj_uint16_t)port);
816 p_pub_addr->sin_addr = *(pj_in_addr*)he.h_addr;
817 }
818
819 *p_sock = sock;
820
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000821 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
822 pj_inet_ntoa(p_pub_addr->sin_addr),
823 (int)pj_ntohs(p_pub_addr->sin_port)));
824
Benny Prijonod8410532006-06-15 11:04:33 +0000825 return PJ_SUCCESS;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000826}
827
828
829/*
830 * Create SIP transport.
831 */
832PJ_DEF(pj_status_t) pjsua_transport_create( pjsip_transport_type_e type,
833 const pjsua_transport_config *cfg,
834 pjsua_transport_id *p_id)
835{
836 pjsip_transport *tp;
837 unsigned id;
838 pj_status_t status;
839
840 PJSUA_LOCK();
841
842 /* Find empty transport slot */
843 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000844 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000845 break;
846 }
847
848 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
849 status = PJ_ETOOMANY;
850 pjsua_perror(THIS_FILE, "Error creating transport", status);
851 goto on_return;
852 }
853
854 /* Create the transport */
855 if (type == PJSIP_TRANSPORT_UDP) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000856 /*
857 * Create UDP transport.
858 */
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000859 pjsua_transport_config config;
Benny Prijonod8410532006-06-15 11:04:33 +0000860 pj_sock_t sock = PJ_INVALID_SOCKET;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000861 pj_sockaddr_in pub_addr;
862 pjsip_host_port addr_name;
863
864 /* Supply default config if it's not specified */
865 if (cfg == NULL) {
866 pjsua_transport_config_default(&config);
867 cfg = &config;
868 }
869
870 /* Create the socket and possibly resolve the address with STUN */
871 status = create_sip_udp_sock(cfg->ip_addr, cfg->port, cfg->use_stun,
872 &cfg->stun_config, &sock, &pub_addr);
873 if (status != PJ_SUCCESS)
874 goto on_return;
875
876 addr_name.host = pj_str(pj_inet_ntoa(pub_addr.sin_addr));
877 addr_name.port = pj_ntohs(pub_addr.sin_port);
878
879 /* Create UDP transport */
880 status = pjsip_udp_transport_attach( pjsua_var.endpt, sock,
881 &addr_name, 1,
882 &tp);
883 if (status != PJ_SUCCESS) {
884 pjsua_perror(THIS_FILE, "Error creating SIP UDP transport",
885 status);
886 pj_sock_close(sock);
887 goto on_return;
888 }
889
Benny Prijonoe93e2872006-06-28 16:46:49 +0000890
891 /* Save the transport */
892 pjsua_var.tpdata[id].type = type;
893 pjsua_var.tpdata[id].local_name = tp->local_name;
894 pjsua_var.tpdata[id].data.tp = tp;
895
896 } else if (type == PJSIP_TRANSPORT_TCP) {
897 /*
898 * Create TCP transport.
899 */
900 pjsua_transport_config config;
901 pjsip_tpfactory *tcp;
902 pj_sockaddr_in local_addr;
903
904 /* Supply default config if it's not specified */
905 if (cfg == NULL) {
906 pjsua_transport_config_default(&config);
907 cfg = &config;
908 }
909
910 /* Init local address */
911 pj_sockaddr_in_init(&local_addr, 0, 0);
912
913 if (cfg->port)
914 local_addr.sin_port = pj_htons((pj_uint16_t)cfg->port);
915
916 if (cfg->ip_addr.s_addr)
917 local_addr.sin_addr.s_addr = cfg->ip_addr.s_addr;
918
919 /* Create the TCP transport */
920 status = pjsip_tcp_transport_start(pjsua_var.endpt, &local_addr, 1,
921 &tcp);
922
923 if (status != PJ_SUCCESS) {
924 pjsua_perror(THIS_FILE, "Error creating SIP TCP listener",
925 status);
926 goto on_return;
927 }
928
929 /* Save the transport */
930 pjsua_var.tpdata[id].type = type;
931 pjsua_var.tpdata[id].local_name = tcp->addr_name;
932 pjsua_var.tpdata[id].data.factory = tcp;
933
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000934 } else {
935 status = PJSIP_EUNSUPTRANSPORT;
936 pjsua_perror(THIS_FILE, "Error creating transport", status);
937 goto on_return;
938 }
939
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000940
941 /* Return the ID */
942 if (p_id) *p_id = id;
943
944 status = PJ_SUCCESS;
945
946on_return:
947
948 PJSUA_UNLOCK();
949
Benny Prijonod8410532006-06-15 11:04:33 +0000950 return status;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000951}
952
953
954/*
955 * Register transport that has been created by application.
956 */
957PJ_DEF(pj_status_t) pjsua_transport_register( pjsip_transport *tp,
958 pjsua_transport_id *p_id)
959{
960 unsigned id;
961
962 PJSUA_LOCK();
963
964 /* Find empty transport slot */
965 for (id=0; id < PJ_ARRAY_SIZE(pjsua_var.tpdata); ++id) {
Benny Prijonoe93e2872006-06-28 16:46:49 +0000966 if (pjsua_var.tpdata[id].data.ptr == NULL)
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000967 break;
968 }
969
970 if (id == PJ_ARRAY_SIZE(pjsua_var.tpdata)) {
971 pjsua_perror(THIS_FILE, "Error creating transport", PJ_ETOOMANY);
972 PJSUA_UNLOCK();
973 return PJ_ETOOMANY;
974 }
975
976 /* Save the transport */
Benny Prijonoe93e2872006-06-28 16:46:49 +0000977 pjsua_var.tpdata[id].type = tp->key.type;
978 pjsua_var.tpdata[id].local_name = tp->local_name;
979 pjsua_var.tpdata[id].data.tp = tp;
Benny Prijonoeebe9af2006-06-13 22:57:13 +0000980
981 /* Return the ID */
982 if (p_id) *p_id = id;
983
984 PJSUA_UNLOCK();
985
986 return PJ_SUCCESS;
987}
988
989
990/*
991 * Enumerate all transports currently created in the system.
992 */
993PJ_DEF(pj_status_t) pjsua_enum_transports( pjsua_transport_id id[],
994 unsigned *p_count )
995{
996 unsigned i, count;
997
998 PJSUA_LOCK();
999
1000 for (i=0, count=0; i<PJ_ARRAY_SIZE(pjsua_var.tpdata) && count<*p_count;
1001 ++i)
1002 {
Benny Prijonoe93e2872006-06-28 16:46:49 +00001003 if (!pjsua_var.tpdata[i].data.ptr)
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001004 continue;
1005
1006 id[count++] = i;
1007 }
1008
1009 *p_count = count;
1010
1011 PJSUA_UNLOCK();
1012
1013 return PJ_SUCCESS;
1014}
1015
1016
1017/*
1018 * Get information about transports.
1019 */
1020PJ_DEF(pj_status_t) pjsua_transport_get_info( pjsua_transport_id id,
1021 pjsua_transport_info *info)
1022{
Benny Prijonoe93e2872006-06-28 16:46:49 +00001023 struct transport_data *t = &pjsua_var.tpdata[id];
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001024
Benny Prijonoac623b32006-07-03 15:19:31 +00001025 pj_bzero(info, sizeof(*info));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001026
1027 /* Make sure id is in range. */
1028 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1029
1030 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001031 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001032
1033 PJSUA_LOCK();
1034
Benny Prijonoe93e2872006-06-28 16:46:49 +00001035 if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_UDP) {
1036
1037 pjsip_transport *tp = t->data.tp;
1038
1039 if (tp == NULL) {
1040 PJSUA_UNLOCK();
1041 return PJ_EINVALIDOP;
1042 }
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001043
Benny Prijonoe93e2872006-06-28 16:46:49 +00001044 info->id = id;
1045 info->type = tp->key.type;
1046 info->type_name = pj_str(tp->type_name);
1047 info->info = pj_str(tp->info);
1048 info->flag = tp->flag;
1049 info->addr_len = tp->addr_len;
1050 info->local_addr = tp->local_addr;
1051 info->local_name = tp->local_name;
1052 info->usage_count = pj_atomic_get(tp->ref_cnt);
1053
1054 } else if (pjsua_var.tpdata[id].type == PJSIP_TRANSPORT_TCP) {
1055
1056 pjsip_tpfactory *factory = t->data.factory;
1057
1058 if (factory == NULL) {
1059 PJSUA_UNLOCK();
1060 return PJ_EINVALIDOP;
1061 }
1062
1063 info->id = id;
1064 info->type = t->type;
1065 info->type_name = pj_str("TCP");
1066 info->info = pj_str("TCP transport");
1067 info->flag = factory->flag;
1068 info->addr_len = sizeof(factory->local_addr);
1069 info->local_addr = factory->local_addr;
1070 info->local_name = factory->addr_name;
1071 info->usage_count = 0;
1072
1073 }
1074
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001075
1076 PJSUA_UNLOCK();
1077
1078 return PJ_EINVALIDOP;
1079}
1080
1081
1082/*
1083 * Disable a transport or re-enable it.
1084 */
1085PJ_DEF(pj_status_t) pjsua_transport_set_enable( pjsua_transport_id id,
1086 pj_bool_t enabled)
1087{
1088 /* Make sure id is in range. */
1089 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1090
1091 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001092 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001093
1094
1095 /* To be done!! */
1096 PJ_TODO(pjsua_transport_set_enable);
Benny Prijonoc570f2d2006-07-18 00:33:02 +00001097 PJ_UNUSED_ARG(enabled);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001098
1099 return PJ_EINVALIDOP;
1100}
1101
1102
1103/*
1104 * Close the transport.
1105 */
1106PJ_DEF(pj_status_t) pjsua_transport_close( pjsua_transport_id id,
1107 pj_bool_t force )
1108{
1109 /* Make sure id is in range. */
1110 PJ_ASSERT_RETURN(id>=0 && id<PJ_ARRAY_SIZE(pjsua_var.tpdata), PJ_EINVAL);
1111
1112 /* Make sure that transport exists */
Benny Prijonoe93e2872006-06-28 16:46:49 +00001113 PJ_ASSERT_RETURN(pjsua_var.tpdata[id].data.ptr != NULL, PJ_EINVAL);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001114
1115
1116 /* To be done!! */
Benny Prijono00cae612006-07-31 15:19:36 +00001117 PJ_UNUSED_ARG(force);
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001118
1119 PJ_TODO(pjsua_transport_close);
1120
1121 return PJ_EINVALIDOP;
1122}
1123
1124
1125/*
1126 * Add additional headers etc in msg_data specified by application
1127 * when sending requests.
1128 */
1129void pjsua_process_msg_data(pjsip_tx_data *tdata,
1130 const pjsua_msg_data *msg_data)
1131{
1132 pj_bool_t allow_body;
1133 const pjsip_hdr *hdr;
1134
Benny Prijono56315612006-07-18 14:39:40 +00001135 /* Always add User-Agent */
1136 if (pjsua_var.ua_cfg.user_agent.slen &&
1137 tdata->msg->type == PJSIP_REQUEST_MSG)
1138 {
1139 const pj_str_t STR_USER_AGENT = { "User-Agent", 10 };
1140 pjsip_hdr *h;
1141 h = (pjsip_hdr*)pjsip_generic_string_hdr_create(tdata->pool,
1142 &STR_USER_AGENT,
1143 &pjsua_var.ua_cfg.user_agent);
1144 pjsip_msg_add_hdr(tdata->msg, h);
1145 }
1146
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001147 if (!msg_data)
1148 return;
1149
1150 hdr = msg_data->hdr_list.next;
1151 while (hdr && hdr != &msg_data->hdr_list) {
1152 pjsip_hdr *new_hdr;
1153
1154 new_hdr = pjsip_hdr_clone(tdata->pool, hdr);
1155 pjsip_msg_add_hdr(tdata->msg, new_hdr);
1156
1157 hdr = hdr->next;
1158 }
1159
1160 allow_body = (tdata->msg->body == NULL);
1161
1162 if (allow_body && msg_data->content_type.slen && msg_data->msg_body.slen) {
1163 pjsip_media_type ctype;
1164 pjsip_msg_body *body;
1165
1166 pjsua_parse_media_type(tdata->pool, &msg_data->content_type, &ctype);
1167 body = pjsip_msg_body_create(tdata->pool, &ctype.type, &ctype.subtype,
1168 &msg_data->msg_body);
1169 tdata->msg->body = body;
1170 }
1171}
1172
1173
1174/*
1175 * Add route_set to outgoing requests
1176 */
1177void pjsua_set_msg_route_set( pjsip_tx_data *tdata,
1178 const pjsip_route_hdr *route_set )
1179{
1180 const pjsip_route_hdr *r;
1181
1182 r = route_set->next;
1183 while (r != route_set) {
1184 pjsip_route_hdr *new_r;
1185
1186 new_r = pjsip_hdr_clone(tdata->pool, r);
1187 pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)new_r);
1188
1189 r = r->next;
1190 }
1191}
1192
1193
1194/*
1195 * Simple version of MIME type parsing (it doesn't support parameters)
1196 */
1197void pjsua_parse_media_type( pj_pool_t *pool,
1198 const pj_str_t *mime,
1199 pjsip_media_type *media_type)
1200{
1201 pj_str_t tmp;
1202 char *pos;
1203
Benny Prijonoac623b32006-07-03 15:19:31 +00001204 pj_bzero(media_type, sizeof(*media_type));
Benny Prijonoeebe9af2006-06-13 22:57:13 +00001205
1206 pj_strdup_with_null(pool, &tmp, mime);
1207
1208 pos = pj_strchr(&tmp, '/');
1209 if (pos) {
1210 media_type->type.ptr = tmp.ptr;
1211 media_type->type.slen = (pos-tmp.ptr);
1212 media_type->subtype.ptr = pos+1;
1213 media_type->subtype.slen = tmp.ptr+tmp.slen-pos-1;
1214 } else {
1215 media_type->type = tmp;
1216 }
1217}
1218
1219
1220/*
1221 * Verify that valid SIP url is given.
1222 */
1223PJ_DEF(pj_status_t) pjsua_verify_sip_url(const char *c_url)
1224{
1225 pjsip_uri *p;
1226 pj_pool_t *pool;
1227 char *url;
1228 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
1229
1230 if (!len) return -1;
1231
1232 pool = pj_pool_create(&pjsua_var.cp.factory, "check%p", 1024, 0, NULL);
1233 if (!pool) return -1;
1234
1235 url = pj_pool_alloc(pool, len+1);
1236 pj_ansi_strcpy(url, c_url);
1237
1238 p = pjsip_parse_uri(pool, url, len, 0);
1239 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
1240 p = NULL;
1241
1242 pj_pool_release(pool);
1243 return p ? 0 : -1;
1244}