blob: c8b0adac951d69ba226a64b42934016425b87102 [file] [log] [blame]
Benny Prijonof3195072006-02-14 21:15:30 +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>
20#include <pjsua-lib/getopt.h>
Benny Prijonof3195072006-02-14 21:15:30 +000021
22#define THIS_FILE "pjsua_opt.c"
23
24
25const char *pjsua_inv_state_names[] =
26{
27 "NULL ",
28 "CALLING ",
29 "INCOMING ",
30 "EARLY ",
31 "CONNECTING",
32 "CONFIRMED ",
33 "DISCONNCTD",
34 "TERMINATED",
35};
36
37
38
39/* Show usage */
40static void usage(void)
41{
42 puts("Usage:");
Benny Prijono64f851e2006-02-23 13:49:28 +000043 puts(" pjsua [options]");
Benny Prijonof3195072006-02-14 21:15:30 +000044 puts("");
Benny Prijonof3195072006-02-14 21:15:30 +000045 puts("General options:");
Benny Prijono64f851e2006-02-23 13:49:28 +000046 puts(" --help Display this help screen");
47 puts(" --version Display version info");
48 puts("");
49 puts("Logging options:");
Benny Prijonof3195072006-02-14 21:15:30 +000050 puts(" --config-file=file Read the config/arguments from file.");
51 puts(" --log-file=fname Log to filename (default stderr)");
52 puts(" --log-level=N Set log max level to N (0(none) to 6(trace))");
53 puts(" --app-log-level=N Set log max level for stdout display to N");
Benny Prijonof3195072006-02-14 21:15:30 +000054 puts("");
Benny Prijonoa91a0032006-02-26 21:23:45 +000055 puts("SIP Account options:");
56 puts(" --id=url Set the URL of local ID (used in From header)");
57 puts(" --contact=url Override the Contact information");
58 puts(" --proxy=url Set the URL of proxy server");
59 puts("");
60 puts("SIP Account Registration Options:");
61 puts(" --registrar=url Set the URL of registrar server");
62 puts(" --reg-timeout=secs Set registration interval to secs (default 3600)");
63 puts("");
64 puts("SIP Account Control:");
65 puts(" --next-account Add more account");
66 puts("");
Benny Prijonof3195072006-02-14 21:15:30 +000067 puts("Authentication options:");
68 puts(" --realm=string Set realm");
69 puts(" --username=string Set authentication username");
70 puts(" --password=string Set authentication password");
Benny Prijonoa91a0032006-02-26 21:23:45 +000071 puts(" --next-cred Add more credential");
Benny Prijono64f851e2006-02-23 13:49:28 +000072 puts("");
73 puts("Transport Options:");
Benny Prijonoa91a0032006-02-26 21:23:45 +000074 puts(" --local-port=port Set TCP/UDP port");
75 puts(" --outbound=url Set the URL of outbound proxy server");
Benny Prijonof3195072006-02-14 21:15:30 +000076 puts(" --use-stun1=host[:port]");
Benny Prijonoa91a0032006-02-26 21:23:45 +000077 puts(" --use-stun2=host[:port] Resolve local IP with the specified STUN servers");
Benny Prijonof3195072006-02-14 21:15:30 +000078 puts("");
Benny Prijono64f851e2006-02-23 13:49:28 +000079 puts("Media Options:");
80 puts(" --null-audio Use NULL audio device");
Benny Prijonoa91a0032006-02-26 21:23:45 +000081 puts(" --play-file=file Play WAV file in conference bridge");
82 puts(" --auto-play Automatically play the file (to incoming calls only)");
83 puts(" --auto-loop Automatically loop incoming RTP to outgoing RTP");
84 puts(" --auto-conf Automatically put incoming calls to conference");
Benny Prijonocbf37402006-03-01 19:29:10 +000085 puts(" --rtp-port=N Base port to try for RTP");
Benny Prijono64f851e2006-02-23 13:49:28 +000086 puts("");
87 puts("Buddy List (can be more than one):");
Benny Prijonof3195072006-02-14 21:15:30 +000088 puts(" --add-buddy url Add the specified URL to the buddy list.");
Benny Prijono64f851e2006-02-23 13:49:28 +000089 puts("");
90 puts("User Agent options:");
91 puts(" --auto-answer=code Automatically answer incoming calls with code (e.g. 200)");
Benny Prijonoa91a0032006-02-26 21:23:45 +000092 puts(" --max-calls=N Maximum number of concurrent calls (default:4, max:255)");
Benny Prijonof3195072006-02-14 21:15:30 +000093 puts("");
94 fflush(stdout);
95}
96
97
98
99/*
100 * Verify that valid SIP url is given.
101 */
102pj_status_t pjsua_verify_sip_url(const char *c_url)
103{
104 pjsip_uri *p;
105 pj_pool_t *pool;
106 char *url;
107 int len = (c_url ? pj_ansi_strlen(c_url) : 0);
108
109 if (!len) return -1;
110
111 pool = pj_pool_create(&pjsua.cp.factory, "check%p", 1024, 0, NULL);
112 if (!pool) return -1;
113
114 url = pj_pool_alloc(pool, len+1);
115 pj_ansi_strcpy(url, c_url);
116
117 p = pjsip_parse_uri(pool, url, len, 0);
118 if (!p || pj_stricmp2(pjsip_uri_get_scheme(p), "sip") != 0)
119 p = NULL;
120
121 pj_pool_release(pool);
122 return p ? 0 : -1;
123}
124
125
126/*
127 * Read command arguments from config file.
128 */
129static int read_config_file(pj_pool_t *pool, const char *filename,
130 int *app_argc, char ***app_argv)
131{
132 int i;
133 FILE *fhnd;
134 char line[200];
135 int argc = 0;
136 char **argv;
137 enum { MAX_ARGS = 64 };
138
139 /* Allocate MAX_ARGS+1 (argv needs to be terminated with NULL argument) */
140 argv = pj_pool_calloc(pool, MAX_ARGS+1, sizeof(char*));
141 argv[argc++] = *app_argv[0];
142
143 /* Open config file. */
144 fhnd = fopen(filename, "rt");
145 if (!fhnd) {
146 printf("Unable to open config file %s\n", filename);
147 fflush(stdout);
148 return -1;
149 }
150
151 /* Scan tokens in the file. */
152 while (argc < MAX_ARGS && !feof(fhnd)) {
153 char *token, *p = line;
154
155 if (fgets(line, sizeof(line), fhnd) == NULL) break;
156
157 for (token = strtok(p, " \t\r\n"); argc < MAX_ARGS;
158 token = strtok(NULL, " \t\r\n"))
159 {
160 int token_len;
161
162 if (!token) break;
163 if (*token == '#') break;
164
165 token_len = strlen(token);
166 if (!token_len)
167 continue;
168 argv[argc] = pj_pool_alloc(pool, token_len+1);
169 pj_memcpy(argv[argc], token, token_len+1);
170 ++argc;
171 }
172 }
173
174 /* Copy arguments from command line */
175 for (i=1; i<*app_argc && argc < MAX_ARGS; ++i)
176 argv[argc++] = (*app_argv)[i];
177
178 if (argc == MAX_ARGS && (i!=*app_argc || !feof(fhnd))) {
179 printf("Too many arguments specified in cmd line/config file\n");
180 fflush(stdout);
181 fclose(fhnd);
182 return -1;
183 }
184
185 fclose(fhnd);
186
187 /* Assign the new command line back to the original command line. */
188 *app_argc = argc;
189 *app_argv = argv;
190 return 0;
191
192}
193
Benny Prijono4f9f64e2006-02-27 00:00:30 +0000194static int my_atoi(const char *cs)
195{
196 pj_str_t s;
197 return pj_strtoul(pj_cstr(&s, cs));
198}
199
Benny Prijonof3195072006-02-14 21:15:30 +0000200
201/* Parse arguments. */
202pj_status_t pjsua_parse_args(int argc, char *argv[])
203{
204 int c;
205 int option_index;
206 enum { OPT_CONFIG_FILE, OPT_LOG_FILE, OPT_LOG_LEVEL, OPT_APP_LOG_LEVEL,
207 OPT_HELP, OPT_VERSION, OPT_NULL_AUDIO,
208 OPT_LOCAL_PORT, OPT_PROXY, OPT_OUTBOUND_PROXY, OPT_REGISTRAR,
209 OPT_REG_TIMEOUT, OPT_ID, OPT_CONTACT,
210 OPT_REALM, OPT_USERNAME, OPT_PASSWORD,
211 OPT_USE_STUN1, OPT_USE_STUN2,
212 OPT_ADD_BUDDY, OPT_OFFER_X_MS_MSG, OPT_NO_PRESENCE,
Benny Prijonoa91a0032006-02-26 21:23:45 +0000213 OPT_AUTO_ANSWER, OPT_AUTO_HANGUP, OPT_AUTO_PLAY, OPT_AUTO_LOOP,
214 OPT_AUTO_CONF,
Benny Prijonocbf37402006-03-01 19:29:10 +0000215 OPT_PLAY_FILE, OPT_RTP_PORT,
Benny Prijonoa91a0032006-02-26 21:23:45 +0000216 OPT_NEXT_ACCOUNT, OPT_NEXT_CRED, OPT_MAX_CALLS,
217 };
Benny Prijonof3195072006-02-14 21:15:30 +0000218 struct option long_options[] = {
219 { "config-file",1, 0, OPT_CONFIG_FILE},
220 { "log-file", 1, 0, OPT_LOG_FILE},
221 { "log-level", 1, 0, OPT_LOG_LEVEL},
222 { "app-log-level",1,0,OPT_APP_LOG_LEVEL},
223 { "help", 0, 0, OPT_HELP},
224 { "version", 0, 0, OPT_VERSION},
225 { "null-audio", 0, 0, OPT_NULL_AUDIO},
226 { "local-port", 1, 0, OPT_LOCAL_PORT},
227 { "proxy", 1, 0, OPT_PROXY},
228 { "outbound", 1, 0, OPT_OUTBOUND_PROXY},
229 { "registrar", 1, 0, OPT_REGISTRAR},
230 { "reg-timeout",1, 0, OPT_REG_TIMEOUT},
231 { "id", 1, 0, OPT_ID},
232 { "contact", 1, 0, OPT_CONTACT},
233 { "realm", 1, 0, OPT_REALM},
234 { "username", 1, 0, OPT_USERNAME},
235 { "password", 1, 0, OPT_PASSWORD},
236 { "use-stun1", 1, 0, OPT_USE_STUN1},
237 { "use-stun2", 1, 0, OPT_USE_STUN2},
238 { "add-buddy", 1, 0, OPT_ADD_BUDDY},
239 { "offer-x-ms-msg",0,0,OPT_OFFER_X_MS_MSG},
240 { "no-presence", 0, 0, OPT_NO_PRESENCE},
241 { "auto-answer",1, 0, OPT_AUTO_ANSWER},
242 { "auto-hangup",1, 0, OPT_AUTO_HANGUP},
Benny Prijonoa91a0032006-02-26 21:23:45 +0000243 { "auto-play", 0, 0, OPT_AUTO_PLAY},
244 { "auto-loop", 0, 0, OPT_AUTO_LOOP},
245 { "auto-conf", 0, 0, OPT_AUTO_CONF},
246 { "play-file", 1, 0, OPT_PLAY_FILE},
Benny Prijonocbf37402006-03-01 19:29:10 +0000247 { "rtp-port", 1, 0, OPT_RTP_PORT},
Benny Prijonoa91a0032006-02-26 21:23:45 +0000248 { "next-account",0,0, OPT_NEXT_ACCOUNT},
249 { "next-cred", 0, 0, OPT_NEXT_CRED},
250 { "max-calls", 1, 0, OPT_MAX_CALLS},
Benny Prijonof3195072006-02-14 21:15:30 +0000251 { NULL, 0, 0, 0}
252 };
253 pj_status_t status;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000254 pjsua_acc *cur_acc;
255 pjsip_cred_info *cur_cred;
Benny Prijonof3195072006-02-14 21:15:30 +0000256 char *config_file = NULL;
257
258 /* Run getopt once to see if user specifies config file to read. */
259 while ((c=getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
260 switch (c) {
261 case OPT_CONFIG_FILE:
262 config_file = optarg;
263 break;
264 }
265 if (config_file)
266 break;
267 }
268
269 if (config_file) {
270 status = read_config_file(pjsua.pool, config_file, &argc, &argv);
271 if (status != 0)
272 return status;
273 }
274
275
Benny Prijonoa91a0032006-02-26 21:23:45 +0000276 cur_acc = &pjsua.acc[0];
277 cur_cred = &pjsua.cred_info[0];
278
279
Benny Prijonof3195072006-02-14 21:15:30 +0000280 /* Reinitialize and re-run getopt again, possibly with new arguments
281 * read from config file.
282 */
283 optind = 0;
284 while ((c=getopt_long(argc, argv, "", long_options, &option_index)) != -1) {
285 char *p;
286 pj_str_t tmp;
287 long lval;
288
289 switch (c) {
290
291 case OPT_LOG_FILE:
292 pjsua.log_filename = optarg;
293 break;
294
295 case OPT_LOG_LEVEL:
296 c = pj_strtoul(pj_cstr(&tmp, optarg));
297 if (c < 0 || c > 6) {
298 printf("Error: expecting integer value 0-6 for --log-level\n");
299 return PJ_EINVAL;
300 }
301 pj_log_set_level( c );
302 break;
303
304 case OPT_APP_LOG_LEVEL:
305 pjsua.app_log_level = pj_strtoul(pj_cstr(&tmp, optarg));
306 if (pjsua.app_log_level < 0 || pjsua.app_log_level > 6) {
307 printf("Error: expecting integer value 0-6 for --app-log-level\n");
308 return PJ_EINVAL;
309 }
310 break;
311
312 case OPT_HELP:
313 usage();
314 return PJ_EINVAL;
315
316 case OPT_VERSION: /* version */
317 pj_dump_config();
318 return PJ_EINVAL;
319
320 case OPT_NULL_AUDIO:
321 pjsua.null_audio = 1;
322 break;
323
324 case OPT_LOCAL_PORT: /* local-port */
325 lval = pj_strtoul(pj_cstr(&tmp, optarg));
326 if (lval < 1 || lval > 65535) {
327 printf("Error: expecting integer value for --local-port\n");
328 return PJ_EINVAL;
329 }
330 pjsua.sip_port = (pj_uint16_t)lval;
331 break;
332
333 case OPT_PROXY: /* proxy */
334 if (pjsua_verify_sip_url(optarg) != 0) {
335 printf("Error: invalid SIP URL '%s' in proxy argument\n", optarg);
336 return PJ_EINVAL;
337 }
Benny Prijonoa91a0032006-02-26 21:23:45 +0000338 cur_acc->proxy = pj_str(optarg);
Benny Prijonof3195072006-02-14 21:15:30 +0000339 break;
340
341 case OPT_OUTBOUND_PROXY: /* outbound proxy */
342 if (pjsua_verify_sip_url(optarg) != 0) {
343 printf("Error: invalid SIP URL '%s' in outbound proxy argument\n", optarg);
344 return PJ_EINVAL;
345 }
346 pjsua.outbound_proxy = pj_str(optarg);
347 break;
348
349 case OPT_REGISTRAR: /* registrar */
350 if (pjsua_verify_sip_url(optarg) != 0) {
351 printf("Error: invalid SIP URL '%s' in registrar argument\n", optarg);
352 return PJ_EINVAL;
353 }
Benny Prijonoa91a0032006-02-26 21:23:45 +0000354 cur_acc->reg_uri = pj_str(optarg);
Benny Prijonof3195072006-02-14 21:15:30 +0000355 break;
356
357 case OPT_REG_TIMEOUT: /* reg-timeout */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000358 cur_acc->reg_timeout = pj_strtoul(pj_cstr(&tmp,optarg));
359 if (cur_acc->reg_timeout < 1 || cur_acc->reg_timeout > 3600) {
Benny Prijonof3195072006-02-14 21:15:30 +0000360 printf("Error: invalid value for --reg-timeout (expecting 1-3600)\n");
361 return PJ_EINVAL;
362 }
363 break;
364
365 case OPT_ID: /* id */
366 if (pjsua_verify_sip_url(optarg) != 0) {
367 printf("Error: invalid SIP URL '%s' in local id argument\n", optarg);
368 return PJ_EINVAL;
369 }
Benny Prijonoa91a0032006-02-26 21:23:45 +0000370 cur_acc->local_uri = pj_str(optarg);
Benny Prijonof3195072006-02-14 21:15:30 +0000371 break;
372
373 case OPT_CONTACT: /* contact */
374 if (pjsua_verify_sip_url(optarg) != 0) {
375 printf("Error: invalid SIP URL '%s' in contact argument\n", optarg);
376 return PJ_EINVAL;
377 }
Benny Prijonoa91a0032006-02-26 21:23:45 +0000378 cur_acc->contact_uri = pj_str(optarg);
379 break;
380
381 case OPT_NEXT_ACCOUNT: /* Add more account. */
382 pjsua.acc_cnt++;
383 cur_acc = &pjsua.acc[pjsua.acc_cnt - 1];
Benny Prijonof3195072006-02-14 21:15:30 +0000384 break;
385
386 case OPT_USERNAME: /* Default authentication user */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000387 if (pjsua.cred_count==0) pjsua.cred_count=1;
388 cur_cred->username = pj_str(optarg);
Benny Prijonof3195072006-02-14 21:15:30 +0000389 break;
390
391 case OPT_REALM: /* Default authentication realm. */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000392 if (pjsua.cred_count==0) pjsua.cred_count=1;
393 cur_cred->realm = pj_str(optarg);
Benny Prijonof3195072006-02-14 21:15:30 +0000394 break;
395
396 case OPT_PASSWORD: /* authentication password */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000397 if (pjsua.cred_count==0) pjsua.cred_count=1;
398 cur_cred->data_type = 0;
399 cur_cred->data = pj_str(optarg);
400 break;
401
402 case OPT_NEXT_CRED: /* Next credential */
403 pjsua.cred_count++;
404 cur_cred = &pjsua.cred_info[pjsua.cred_count - 1];
Benny Prijonof3195072006-02-14 21:15:30 +0000405 break;
406
407 case OPT_USE_STUN1: /* STUN server 1 */
408 p = pj_ansi_strchr(optarg, ':');
409 if (p) {
410 *p = '\0';
411 pjsua.stun_srv1 = pj_str(optarg);
412 pjsua.stun_port1 = pj_strtoul(pj_cstr(&tmp, p+1));
413 if (pjsua.stun_port1 < 1 || pjsua.stun_port1 > 65535) {
414 printf("Error: expecting port number with option --use-stun1\n");
415 return PJ_EINVAL;
416 }
417 } else {
418 pjsua.stun_port1 = 3478;
419 pjsua.stun_srv1 = pj_str(optarg);
420 }
421 break;
422
423 case OPT_USE_STUN2: /* STUN server 2 */
424 p = pj_ansi_strchr(optarg, ':');
425 if (p) {
426 *p = '\0';
427 pjsua.stun_srv2 = pj_str(optarg);
428 pjsua.stun_port2 = pj_strtoul(pj_cstr(&tmp,p+1));
429 if (pjsua.stun_port2 < 1 || pjsua.stun_port2 > 65535) {
430 printf("Error: expecting port number with option --use-stun2\n");
431 return PJ_EINVAL;
432 }
433 } else {
434 pjsua.stun_port2 = 3478;
435 pjsua.stun_srv2 = pj_str(optarg);
436 }
437 break;
438
439 case OPT_ADD_BUDDY: /* Add to buddy list. */
440 if (pjsua_verify_sip_url(optarg) != 0) {
441 printf("Error: invalid URL '%s' in --add-buddy option\n", optarg);
442 return -1;
443 }
444 if (pjsua.buddy_cnt == PJSUA_MAX_BUDDIES) {
445 printf("Error: too many buddies in buddy list.\n");
446 return -1;
447 }
Benny Prijono834aee32006-02-19 01:38:06 +0000448 pjsua.buddies[pjsua.buddy_cnt++].uri = pj_str(optarg);
Benny Prijonof3195072006-02-14 21:15:30 +0000449 break;
Benny Prijono39879152006-02-23 02:09:10 +0000450
Benny Prijono64f851e2006-02-23 13:49:28 +0000451 case OPT_AUTO_PLAY:
Benny Prijonoa91a0032006-02-26 21:23:45 +0000452 pjsua.auto_play = 1;
453 break;
454
455 case OPT_AUTO_LOOP:
456 pjsua.auto_loop = 1;
457 break;
458
459 case OPT_AUTO_CONF:
460 pjsua.auto_conf = 1;
461 break;
462
463 case OPT_PLAY_FILE:
Benny Prijono39879152006-02-23 02:09:10 +0000464 pjsua.wav_file = optarg;
465 break;
Benny Prijono64f851e2006-02-23 13:49:28 +0000466
Benny Prijonocbf37402006-03-01 19:29:10 +0000467 case OPT_RTP_PORT:
468 pjsua.start_rtp_port = my_atoi(optarg);
469 if (pjsua.start_rtp_port < 1 || pjsua.start_rtp_port > 65535) {
470 PJ_LOG(1,(THIS_FILE,
471 "Error: rtp-port argument value (expecting 1-65535"));
472 return -1;
473 }
474
Benny Prijono64f851e2006-02-23 13:49:28 +0000475 case OPT_AUTO_ANSWER:
Benny Prijono4f9f64e2006-02-27 00:00:30 +0000476 pjsua.auto_answer = my_atoi(optarg);
Benny Prijono64f851e2006-02-23 13:49:28 +0000477 if (pjsua.auto_answer < 100 || pjsua.auto_answer > 699) {
Benny Prijonocbf37402006-03-01 19:29:10 +0000478 PJ_LOG(1,(THIS_FILE,
479 "Error: invalid code in --auto-answer (expecting 100-699"));
Benny Prijono64f851e2006-02-23 13:49:28 +0000480 return -1;
481 }
482 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000483
484 case OPT_MAX_CALLS:
Benny Prijono4f9f64e2006-02-27 00:00:30 +0000485 pjsua.max_calls = my_atoi(optarg);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000486 if (pjsua.max_calls < 1 || pjsua.max_calls > 255) {
Benny Prijonocbf37402006-03-01 19:29:10 +0000487 PJ_LOG(1,(THIS_FILE,"Too many calls for max-calls (1-255)"));
Benny Prijonoa91a0032006-02-26 21:23:45 +0000488 return -1;
489 }
490 break;
Benny Prijonof3195072006-02-14 21:15:30 +0000491 }
492 }
493
494 if (optind != argc) {
495 printf("Error: unknown options %s\n", argv[optind]);
496 return PJ_EINVAL;
497 }
498
Benny Prijonof3195072006-02-14 21:15:30 +0000499 return PJ_SUCCESS;
500}
501
502
503
Benny Prijonoa91a0032006-02-26 21:23:45 +0000504static void print_call(const char *title,
505 int call_index,
506 char *buf, pj_size_t size)
Benny Prijonof3195072006-02-14 21:15:30 +0000507{
508 int len;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000509 pjsip_inv_session *inv = pjsua.calls[call_index].inv;
Benny Prijonof3195072006-02-14 21:15:30 +0000510 pjsip_dialog *dlg = inv->dlg;
511 char userinfo[128];
512
513 /* Dump invite sesion info. */
514
515 len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo));
516 if (len < 1)
517 pj_ansi_strcpy(userinfo, "<--uri too long-->");
518 else
519 userinfo[len] = '\0';
520
521 len = pj_snprintf(buf, size, "%s[%s] %s",
522 title,
523 pjsua_inv_state_names[inv->state],
524 userinfo);
525 if (len < 1 || len >= (int)size) {
526 pj_ansi_strcpy(buf, "<--uri too long-->");
527 len = 18;
528 } else
529 buf[len] = '\0';
530}
531
532static void dump_media_session(pjmedia_session *session)
533{
534 unsigned i;
535 pjmedia_session_info info;
536
537 pjmedia_session_get_info(session, &info);
538
539 for (i=0; i<info.stream_cnt; ++i) {
540 pjmedia_stream_stat strm_stat;
541 const char *rem_addr;
542 int rem_port;
543 const char *dir;
544
545 pjmedia_session_get_stream_stat(session, i, &strm_stat);
546 rem_addr = pj_inet_ntoa(info.stream_info[i].rem_addr.sin_addr);
547 rem_port = pj_ntohs(info.stream_info[i].rem_addr.sin_port);
548
549 if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING)
550 dir = "sendonly";
551 else if (info.stream_info[i].dir == PJMEDIA_DIR_DECODING)
552 dir = "recvonly";
553 else if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING_DECODING)
554 dir = "sendrecv";
555 else
556 dir = "inactive";
557
558
559 PJ_LOG(3,(THIS_FILE,
560 "%s[Media strm#%d] %.*s, %s, peer=%s:%d",
561 " ",
562 i,
563 info.stream_info[i].fmt.encoding_name.slen,
564 info.stream_info[i].fmt.encoding_name.ptr,
565 dir,
566 rem_addr, rem_port));
567 PJ_LOG(3,(THIS_FILE,
568 "%s tx {pkt=%u, bytes=%u} rx {pkt=%u, bytes=%u}",
569 " ",
570 strm_stat.enc.pkt, strm_stat.enc.bytes,
571 strm_stat.dec.pkt, strm_stat.dec.bytes));
572
573 }
574}
575
576/*
577 * Dump application states.
578 */
579void pjsua_dump(void)
580{
Benny Prijonof3195072006-02-14 21:15:30 +0000581 char buf[128];
582 unsigned old_decor;
583
584 PJ_LOG(3,(THIS_FILE, "Start dumping application states:"));
585
586 old_decor = pj_log_get_decor();
587 pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR));
588
589 pjsip_endpt_dump(pjsua.endpt, 1);
590 pjmedia_endpt_dump(pjsua.med_endpt);
Benny Prijonocbf37402006-03-01 19:29:10 +0000591 pjsip_tsx_layer_dump(1);
592 pjsip_ua_dump(1);
Benny Prijonof3195072006-02-14 21:15:30 +0000593
594
595 /* Dump all invite sessions: */
596 PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:"));
597
Benny Prijonoa91a0032006-02-26 21:23:45 +0000598 if (pjsua.call_cnt == 0) {
Benny Prijonof3195072006-02-14 21:15:30 +0000599
600 PJ_LOG(3,(THIS_FILE, " - no sessions -"));
601
602 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000603 int i;
Benny Prijonof3195072006-02-14 21:15:30 +0000604
Benny Prijonoa91a0032006-02-26 21:23:45 +0000605 for (i=0; i<pjsua.max_calls; ++i) {
Benny Prijonof3195072006-02-14 21:15:30 +0000606
Benny Prijonoa91a0032006-02-26 21:23:45 +0000607 if (pjsua.calls[i].inv == NULL)
608 continue;
Benny Prijonof3195072006-02-14 21:15:30 +0000609
Benny Prijonoa91a0032006-02-26 21:23:45 +0000610 print_call(" ", i, buf, sizeof(buf));
Benny Prijonof3195072006-02-14 21:15:30 +0000611 PJ_LOG(3,(THIS_FILE, "%s", buf));
612
Benny Prijonoa91a0032006-02-26 21:23:45 +0000613 if (pjsua.calls[i].session)
614 dump_media_session(pjsua.calls[i].session);
Benny Prijonof3195072006-02-14 21:15:30 +0000615 }
616 }
617
Benny Prijono834aee32006-02-19 01:38:06 +0000618 /* Dump presence status */
619 pjsua_pres_dump();
620
Benny Prijonof3195072006-02-14 21:15:30 +0000621 pj_log_set_decor(old_decor);
622 PJ_LOG(3,(THIS_FILE, "Dump complete"));
623}
624
625
626/*
627 * Load settings.
628 */
629pj_status_t pjsua_load_settings(const char *filename)
630{
631 int argc = 3;
Benny Prijono834aee32006-02-19 01:38:06 +0000632 char *argv[4] = { "pjsua", "--config-file", NULL, NULL};
Benny Prijonof3195072006-02-14 21:15:30 +0000633
Benny Prijono834aee32006-02-19 01:38:06 +0000634 argv[3] = (char*)filename;
Benny Prijonof3195072006-02-14 21:15:30 +0000635 return pjsua_parse_args(argc, argv);
636}
637
638
639/*
Benny Prijonoa91a0032006-02-26 21:23:45 +0000640 * Save account settings
Benny Prijonof3195072006-02-14 21:15:30 +0000641 */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000642static void save_account_settings(int acc_index, pj_str_t *result)
Benny Prijonof3195072006-02-14 21:15:30 +0000643{
Benny Prijonof3195072006-02-14 21:15:30 +0000644 char line[128];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000645 pjsua_acc *acc = &pjsua.acc[acc_index];
Benny Prijonof3195072006-02-14 21:15:30 +0000646
Benny Prijonoa91a0032006-02-26 21:23:45 +0000647
648 pj_ansi_sprintf(line, "#\n# Account %d:\n#\n", acc_index);
649 pj_strcat2(result, line);
Benny Prijonof3195072006-02-14 21:15:30 +0000650
651
652 /* Identity */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000653 if (acc->local_uri.slen) {
Benny Prijonof3195072006-02-14 21:15:30 +0000654 pj_ansi_sprintf(line, "--id %.*s\n",
Benny Prijonoa91a0032006-02-26 21:23:45 +0000655 (int)acc->local_uri.slen,
656 acc->local_uri.ptr);
657 pj_strcat2(result, line);
658 }
659
660 /* Registrar server */
661 if (acc->reg_uri.slen) {
662 pj_ansi_sprintf(line, "--registrar %.*s\n",
663 (int)acc->reg_uri.slen,
664 acc->reg_uri.ptr);
665 pj_strcat2(result, line);
666
667 pj_ansi_sprintf(line, "--reg-timeout %u\n",
668 acc->reg_timeout);
669 pj_strcat2(result, line);
670 }
671
672
673 /* Proxy */
674 if (acc->proxy.slen) {
675 pj_ansi_sprintf(line, "--proxy %.*s\n",
676 (int)acc->proxy.slen,
677 acc->proxy.ptr);
678 pj_strcat2(result, line);
679 }
680}
681
682
683
684/*
685 * Dump settings.
686 */
687int pjsua_dump_settings(char *buf, pj_size_t max)
688{
689 int acc_index;
690 int i;
691 pj_str_t cfg;
692 char line[128];
693
694 cfg.ptr = buf;
695 cfg.slen = 0;
696
697
698 /* Logging. */
699 pj_strcat2(&cfg, "#\n# Logging options:\n#\n");
700 pj_ansi_sprintf(line, "--log-level %d\n",
701 pjsua.log_level);
702 pj_strcat2(&cfg, line);
703
704 pj_ansi_sprintf(line, "--app-log-level %d\n",
705 pjsua.app_log_level);
706 pj_strcat2(&cfg, line);
707
708 if (pjsua.log_filename) {
709 pj_ansi_sprintf(line, "--log-file %s\n",
710 pjsua.log_filename);
Benny Prijonof3195072006-02-14 21:15:30 +0000711 pj_strcat2(&cfg, line);
712 }
713
Benny Prijonoa91a0032006-02-26 21:23:45 +0000714
715 /* Save account settings. */
716 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
717
718 save_account_settings(acc_index, &cfg);
719
720 if (acc_index < pjsua.acc_cnt-1)
721 pj_strcat2(&cfg, "--next-account\n");
722 }
723
Benny Prijonof3195072006-02-14 21:15:30 +0000724 /* Credentials. */
725 for (i=0; i<pjsua.cred_count; ++i) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000726
727 pj_ansi_sprintf(line, "#\n# Credential %d:\n#\n", i);
728 pj_strcat2(&cfg, line);
729
Benny Prijonof3195072006-02-14 21:15:30 +0000730 if (pjsua.cred_info[i].realm.slen) {
731 pj_ansi_sprintf(line, "--realm %.*s\n",
732 (int)pjsua.cred_info[i].realm.slen,
733 pjsua.cred_info[i].realm.ptr);
734 pj_strcat2(&cfg, line);
735 }
736
737 pj_ansi_sprintf(line, "--username %.*s\n",
738 (int)pjsua.cred_info[i].username.slen,
739 pjsua.cred_info[i].username.ptr);
740 pj_strcat2(&cfg, line);
741
742 pj_ansi_sprintf(line, "--password %.*s\n",
743 (int)pjsua.cred_info[i].data.slen,
744 pjsua.cred_info[i].data.ptr);
745 pj_strcat2(&cfg, line);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000746
747 if (i < pjsua.cred_count-1)
748 pj_strcat2(&cfg, "--next-cred\n");
Benny Prijonof3195072006-02-14 21:15:30 +0000749 }
750
Benny Prijonof3195072006-02-14 21:15:30 +0000751
Benny Prijonoa91a0032006-02-26 21:23:45 +0000752 pj_strcat2(&cfg, "#\n# Network settings:\n#\n");
Benny Prijonof3195072006-02-14 21:15:30 +0000753
754 /* Outbound proxy */
755 if (pjsua.outbound_proxy.slen) {
756 pj_ansi_sprintf(line, "--outbound %.*s\n",
757 (int)pjsua.outbound_proxy.slen,
758 pjsua.outbound_proxy.ptr);
759 pj_strcat2(&cfg, line);
760 }
761
Benny Prijonof3195072006-02-14 21:15:30 +0000762
763 /* Transport. */
764 pj_ansi_sprintf(line, "--local-port %d\n", pjsua.sip_port);
765 pj_strcat2(&cfg, line);
766
767
768 /* STUN */
769 if (pjsua.stun_port1) {
770 pj_ansi_sprintf(line, "--use-stun1 %.*s:%d\n",
771 (int)pjsua.stun_srv1.slen,
772 pjsua.stun_srv1.ptr,
773 pjsua.stun_port1);
774 pj_strcat2(&cfg, line);
775 }
776
777 if (pjsua.stun_port2) {
778 pj_ansi_sprintf(line, "--use-stun2 %.*s:%d\n",
779 (int)pjsua.stun_srv2.slen,
780 pjsua.stun_srv2.ptr,
781 pjsua.stun_port2);
782 pj_strcat2(&cfg, line);
783 }
784
785
Benny Prijonoa91a0032006-02-26 21:23:45 +0000786 pj_strcat2(&cfg, "#\n# Media settings:\n#\n");
787
788
789 /* Media */
790 if (pjsua.null_audio)
791 pj_strcat2(&cfg, "--null-audio\n");
792 if (pjsua.auto_play)
793 pj_strcat2(&cfg, "--auto-play\n");
794 if (pjsua.auto_loop)
795 pj_strcat2(&cfg, "--auto-loop\n");
796 if (pjsua.auto_conf)
797 pj_strcat2(&cfg, "--auto-conf\n");
798 if (pjsua.wav_file) {
799 pj_ansi_sprintf(line, "--play-file %s\n",
800 pjsua.wav_file);
801 pj_strcat2(&cfg, line);
802 }
803
804
805 pj_strcat2(&cfg, "#\n# User agent:\n#\n");
806
807 /* Auto-answer. */
808 if (pjsua.auto_answer != 0) {
809 pj_ansi_sprintf(line, "--auto-answer %d\n",
810 pjsua.auto_answer);
811 pj_strcat2(&cfg, line);
812 }
813
814 /* Max calls. */
815 pj_ansi_sprintf(line, "--max-calls %d\n",
816 pjsua.max_calls);
817 pj_strcat2(&cfg, line);
818
819
820 pj_strcat2(&cfg, "#\n# Buddies:\n#\n");
821
Benny Prijonof3195072006-02-14 21:15:30 +0000822 /* Add buddies. */
823 for (i=0; i<pjsua.buddy_cnt; ++i) {
824 pj_ansi_sprintf(line, "--add-buddy %.*s\n",
Benny Prijono834aee32006-02-19 01:38:06 +0000825 (int)pjsua.buddies[i].uri.slen,
826 pjsua.buddies[i].uri.ptr);
Benny Prijonof3195072006-02-14 21:15:30 +0000827 pj_strcat2(&cfg, line);
828 }
829
830
Benny Prijonoa91a0032006-02-26 21:23:45 +0000831 *(cfg.ptr + cfg.slen) = '\0';
832 return cfg.slen;
833}
834
835/*
836 * Save settings.
837 */
838pj_status_t pjsua_save_settings(const char *filename)
839{
840 pj_str_t cfg;
841 pj_pool_t *pool;
842 FILE *fhnd;
843
844 /* Create pool for temporary buffer. */
845 pool = pj_pool_create(&pjsua.cp.factory, "settings", 4000, 0, NULL);
846 if (!pool)
847 return PJ_ENOMEM;
848
849
850 cfg.ptr = pj_pool_alloc(pool, 3800);
851 if (!cfg.ptr) {
852 pj_pool_release(pool);
853 return PJ_EBUG;
854 }
855
856
857 cfg.slen = pjsua_dump_settings(cfg.ptr, 3800);
858 if (cfg.slen < 1) {
859 pj_pool_release(pool);
860 return PJ_ENOMEM;
861 }
862
863
Benny Prijonof3195072006-02-14 21:15:30 +0000864 /* Write to file. */
865 fhnd = fopen(filename, "wt");
866 if (!fhnd) {
867 pj_pool_release(pool);
868 return pj_get_os_error();
869 }
870
871 fwrite(cfg.ptr, cfg.slen, 1, fhnd);
872 fclose(fhnd);
873
874 pj_pool_release(pool);
875 return PJ_SUCCESS;
876}