Debian Codegraph @

Xpjproject/pjsip-apps/src/samples/sipecho.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
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 */
19
20
21 /**
22 * sipecho.c
23 *
24 * - Accepts incoming calls and echoes back SDP and any media.
25 * - Specify URI in cmdline argument to make call
26 * - Accepts registration too!
27 */
28
29 /* Include all headers. */
30 #include <pjsip.h>
31 #include <pjmedia/sdp.h>
32 #include <pjsip_ua.h>
33 #include <pjlib-util.h>
34 #include <pjlib.h>
35
36 /* For logging purpose. */
37 #define THIS_FILE "sipecho.c"
38
39 #include "util.h"
40
41
42 /* Settings */
43 #define MAX_CALLS 8
44
45 typedef struct call_t
46 {
47 pjsip_inv_session *inv;
48 } call_t;
49
50 static struct app_t
51 {
52 pj_caching_pool cp;
53 pj_pool_t *pool;
54
55 pjsip_endpoint *sip_endpt;
56 //pjmedia_endpt *med_endpt;
57
58 call_t call[MAX_CALLS];
59
60 pj_bool_t quit;
61 pj_thread_t *worker_thread;
62
63 pj_bool_t enable_msg_logging;
64 } app;
65
66 /*
67 * Prototypes:
68 */
69
70 static void call_on_media_update(pjsip_inv_session *inv, pj_status_t status);
71 static void call_on_state_changed(pjsip_inv_session *inv, pjsip_event *e);
72 static void call_on_rx_offer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer);
73 static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
74 static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
75
76 /* Globals */
77 static int sip_af;
78 static int sip_port = 5060;
79 static pj_bool_t sip_tcp;
80
81 /* This is a PJSIP module to be registered by application to handle
82 * incoming requests outside any dialogs/transactions. The main purpose
83 * here is to handle incoming INVITE request message, where we will
84 * create a dialog and INVITE session for it.
85 */
86 static pjsip_module mod_sipecho =
87 {
88 NULL, NULL, /* prev, next. */
89 { "mod-sipecho", 11 }, /* Name. */
90 -1, /* Id */
91 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
92 NULL, /* load() */
93 NULL, /* start() */
94 NULL, /* stop() */
95 NULL, /* unload() */
96 &on_rx_request, /* on_rx_request() */
97 NULL, /* on_rx_response() */
98 NULL, /* on_tx_request. */
99 NULL, /* on_tx_response() */
100 NULL, /* on_tsx_state() */
101 };
102
103 /* Notification on incoming messages */
104 static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata)
105 {
106 if (!app.enable_msg_logging)
107 return PJ_FALSE;
108
109 PJ_LOG(3,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n"
110 "%.*s\n"
111 "--end msg--",
112 rdata->msg_info.len,
113 pjsip_rx_data_get_info(rdata),
114 rdata->tp_info.transport->type_name,
115 rdata->pkt_info.src_name,
116 rdata->pkt_info.src_port,
117 (int)rdata->msg_info.len,
118 rdata->msg_info.msg_buf));
119 return PJ_FALSE;
120 }
121
122 /* Notification on outgoing messages */
123 static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata)
124 {
125 if (!app.enable_msg_logging)
126 return PJ_SUCCESS;
127
128 PJ_LOG(3,(THIS_FILE, "TX %ld bytes %s to %s %s:%d:\n"
129 "%.*s\n"
130 "--end msg--",
131 (tdata->buf.cur - tdata->buf.start),
132 pjsip_tx_data_get_info(tdata),
133 tdata->tp_info.transport->type_name,
134 tdata->tp_info.dst_name,
135 tdata->tp_info.dst_port,
136 (int)(tdata->buf.cur - tdata->buf.start),
137 tdata->buf.start));
138 return PJ_SUCCESS;
139 }
140
141 /* The module instance. */
142 static pjsip_module msg_logger =
143 {
144 NULL, NULL, /* prev, next. */
145 { "mod-msg-log", 13 }, /* Name. */
146 -1, /* Id */
147 PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */
148 NULL, /* load() */
149 NULL, /* start() */
150 NULL, /* stop() */
151 NULL, /* unload() */
152 &logging_on_rx_msg, /* on_rx_request() */
153 &logging_on_rx_msg, /* on_rx_response() */
154 &logging_on_tx_msg, /* on_tx_request. */
155 &logging_on_tx_msg, /* on_tx_response() */
156 NULL, /* on_tsx_state() */
157
158 };
159
160 static int worker_proc(void *arg)
161 {
162 PJ_UNUSED_ARG(arg);
163
164 while (!app.quit) {
165 pj_time_val interval = { 0, 20 };
166 pjsip_endpt_handle_events(app.sip_endpt, &interval);
167 }
168
169 return 0;
170 }
171
172 static void hangup_all(void)
173 {
174 unsigned i;
175 for (i=0; i<MAX_CALLS; ++i) {
176 call_t *call = &app.call[i];
177
178 if (call->inv && call->inv->state <= PJSIP_INV_STATE_CONFIRMED) {
179 pj_status_t status;
180 pjsip_tx_data *tdata;
181
182 status = pjsip_inv_end_session(call->inv, PJSIP_SC_BUSY_HERE, NULL, &tdata);
183 if (status==PJ_SUCCESS && tdata)
184 pjsip_inv_send_msg(call->inv, tdata);
185 }
186 }
187 }
188
189 static void destroy_stack(void)
190 {
191 enum { WAIT_CLEAR = 5000, WAIT_INTERVAL = 500 };
192 unsigned i;
193
194 PJ_LOG(3,(THIS_FILE, "Shutting down.."));
195
196 /* Wait until all clear */
197 hangup_all();
198 for (i=0; i<WAIT_CLEAR/WAIT_INTERVAL; ++i) {
199 unsigned j;
200
201 for (j=0; j<MAX_CALLS; ++j) {
202 call_t *call = &app.call[j];
203 if (call->inv && call->inv->state <= PJSIP_INV_STATE_CONFIRMED)
204 break;
205 }
206
207 if (j==MAX_CALLS)
208 return;
209
210 pj_thread_sleep(WAIT_INTERVAL);
211 }
212
213 app.quit = PJ_TRUE;
214 if (app.worker_thread) {
215 pj_thread_join(app.worker_thread);
216 app.worker_thread = NULL;
217 }
218
219 //if (app.med_endpt)
220 //pjmedia_endpt_destroy(app.med_endpt);
221
222 if (app.sip_endpt)
223 pjsip_endpt_destroy(app.sip_endpt);
224
225 if (app.pool)
226 pj_pool_release(app.pool);
227
228 dump_pool_usage(THIS_FILE, &app.cp);
229 pj_caching_pool_destroy(&app.cp);
230 }
231
232
233 static pj_status_t init_stack()
234 {
235 pj_sockaddr addr;
236 pjsip_inv_callback inv_cb;
237 pj_status_t status;
238
239 pj_log_set_level(3);
240
241 CHECK( pjlib_util_init() );
242
243 pj_caching_pool_init(&app.cp, NULL, 0);
244 app.pool = pj_pool_create( &app.cp.factory, "sipecho", 512, 512, 0);
245
246 CHECK( pjsip_endpt_create(&app.cp.factory, NULL, &app.sip_endpt) );
247
248 pj_log_set_level(4);
249 pj_sockaddr_init((pj_uint16_t)sip_af, &addr, NULL, (pj_uint16_t)sip_port);
250 if (sip_af == pj_AF_INET()) {
251 if (sip_tcp) {
252 CHECK( pjsip_tcp_transport_start( app.sip_endpt, &addr.ipv4, 1,
253 NULL) );
254 } else {
255 CHECK( pjsip_udp_transport_start( app.sip_endpt, &addr.ipv4,
256 NULL, 1, NULL) );
257 }
258 } else if (sip_af == pj_AF_INET6()) {
259 CHECK( pjsip_udp_transport_start6(app.sip_endpt, &addr.ipv6,
260 NULL, 1, NULL) );
261 } else {
262 CHECK( PJ_EAFNOTSUP );
263 }
264
265 pj_log_set_level(3);
266
267 status = pjsip_tsx_layer_init_module(app.sip_endpt) ||
268 pjsip_ua_init_module( app.sip_endpt, NULL );
269 CHECK( status );
270
271 pj_bzero(&inv_cb, sizeof(inv_cb));
272 inv_cb.on_state_changed = &call_on_state_changed;
273 inv_cb.on_new_session = &call_on_forked;
274 inv_cb.on_media_update = &call_on_media_update;
275 inv_cb.on_rx_offer = &call_on_rx_offer;
276
277 status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb) ||
278 pjsip_100rel_init_module(app.sip_endpt) ||
279 pjsip_endpt_register_module( app.sip_endpt, &mod_sipecho) ||
280 pjsip_endpt_register_module( app.sip_endpt, &msg_logger) ||
281 //pjmedia_endpt_create(&app.cp.factory,
282 // pjsip_endpt_get_ioqueue(app.sip_endpt),
283 // 0, &app.med_endpt) ||
284 pj_thread_create(app.pool, "sipecho", &worker_proc, NULL, 0, 0,
285 &app.worker_thread);
286 CHECK( status );
287
288 return PJ_SUCCESS;
289
290 on_error:
291 return status;
292 }
293
294 static void destroy_call(call_t *call)
295 {
296 call->inv = NULL;
297 }
298
299 static pjmedia_sdp_attr * find_remove_sdp_attrs(unsigned *cnt,
300 pjmedia_sdp_attr *attr[],
301 unsigned cnt_attr_to_remove,
302 const char* attr_to_remove[])
303 {
304 pjmedia_sdp_attr *found_attr = NULL;
305 int i;
306
307 for (i=0; i<(int)*cnt; ++i) {
308 unsigned j;
309 for (j=0; j<cnt_attr_to_remove; ++j) {
310 if (pj_strcmp2(&attr[i]->name, attr_to_remove[j])==0) {
311 if (!found_attr) found_attr = attr[i];
312 pj_array_erase(attr, sizeof(attr[0]), *cnt, i);
313 --(*cnt);
314 --i;
315 break;
316 }
317 }
318 }
319
320 return found_attr;
321 }
322
323 static pjmedia_sdp_session *create_answer(int call_num, pj_pool_t *pool,
324 const pjmedia_sdp_session *offer)
325 {
326 const char* dir_attrs[] = { "sendrecv", "sendonly", "recvonly", "inactive" };
327 const char *ice_attrs[] = {"ice-pwd", "ice-ufrag", "candidate"};
328 pjmedia_sdp_session *answer = pjmedia_sdp_session_clone(pool, offer);
329 pjmedia_sdp_attr *sess_dir_attr = NULL;
330 unsigned mi;
331
332 PJ_LOG(3,(THIS_FILE, "Call %d: creating answer:", call_num));
333
334 answer->name = pj_str("sipecho");
335 sess_dir_attr = find_remove_sdp_attrs(&answer->attr_count, answer->attr,
336 PJ_ARRAY_SIZE(dir_attrs),
337 dir_attrs);
338
339 for (mi=0; mi<answer->media_count; ++mi) {
340 pjmedia_sdp_media *m = answer->media[mi];
341 pjmedia_sdp_attr *m_dir_attr;
342 pjmedia_sdp_attr *dir_attr;
343 const char *our_dir = NULL;
344 pjmedia_sdp_conn *c;
345
346 /* Match direction */
347 m_dir_attr = find_remove_sdp_attrs(&m->attr_count, m->attr,
348 PJ_ARRAY_SIZE(dir_attrs),
349 dir_attrs);
350 dir_attr = m_dir_attr ? m_dir_attr : sess_dir_attr;
351
352 if (dir_attr) {
353 if (pj_strcmp2(&dir_attr->name, "sendonly")==0)
354 our_dir = "recvonly";
355 else if (pj_strcmp2(&dir_attr->name, "inactive")==0)
356 our_dir = "inactive";
357 else if (pj_strcmp2(&dir_attr->name, "recvonly")==0)
358 our_dir = "inactive";
359
360 if (our_dir) {
361 dir_attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr);
362 dir_attr->name = pj_str((char*)our_dir);
363 m->attr[m->attr_count++] = dir_attr;
364 }
365 }
366
367 /* Remove ICE attributes */
368 find_remove_sdp_attrs(&m->attr_count, m->attr, PJ_ARRAY_SIZE(ice_attrs), ice_attrs);
369
370 /* Done */
371 c = m->conn ? m->conn : answer->conn;
372 PJ_LOG(3,(THIS_FILE, " Media %d, %.*s: %s <--> %.*s:%d",
373 mi, (int)m->desc.media.slen, m->desc.media.ptr,
374 (our_dir ? our_dir : "sendrecv"),
375 (c? (int)c->addr.slen : 6), (c? c->addr.ptr : "(none)"),
376 m->desc.port));
377 }
378
379 return answer;
380 }
381
382 static void call_on_state_changed( pjsip_inv_session *inv,
383 pjsip_event *e)
384 {
385 call_t *call = (call_t*)inv->mod_data[mod_sipecho.id];
386 if (!call)
387 return;
388
389 PJ_UNUSED_ARG(e);
390 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
391 PJ_LOG(3,(THIS_FILE, "Call %ld: DISCONNECTED [reason=%d (%s)]",
392 call - app.call, inv->cause,
393 pjsip_get_status_text(inv->cause)->ptr));
394 destroy_call(call);
395 } else {
396 PJ_LOG(3,(THIS_FILE, "Call %ld: state changed to %s",
397 call - app.call, pjsip_inv_state_name(inv->state)));
398 }
399 }
400
401 static void call_on_rx_offer(pjsip_inv_session *inv, const pjmedia_sdp_session *offer)
402 {
403 call_t *call = (call_t*) inv->mod_data[mod_sipecho.id];
404 pjsip_inv_set_sdp_answer(inv, create_answer((int)(call - app.call),
405 inv->pool_prov, offer));
406 }
407
408 static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
409 {
410 PJ_UNUSED_ARG(inv);
411 PJ_UNUSED_ARG(e);
412 }
413
414 static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
415 {
416 pj_sockaddr hostaddr;
417 char temp[80], hostip[PJ_INET6_ADDRSTRLEN];
418 pj_str_t local_uri;
419 pjsip_dialog *dlg = NULL;
420 pjsip_rdata_sdp_info *sdp_info;
421 pjmedia_sdp_session *answer = NULL;
422 pjsip_tx_data *tdata = NULL;
423 call_t *call = NULL;
424 unsigned i;
425 pj_status_t status;
426
427 PJ_LOG(3,(THIS_FILE, "RX %.*s from %s",
428 (int)rdata->msg_info.msg->line.req.method.name.slen,
429 rdata->msg_info.msg->line.req.method.name.ptr,
430 rdata->pkt_info.src_name));
431
432 if (rdata->msg_info.msg->line.req.method.id == PJSIP_REGISTER_METHOD) {
433 /* Let me be a registrar! */
434 pjsip_hdr hdr_list, *h;
435 pjsip_msg *msg;
436 unsigned expires = PJSIP_EXPIRES_NOT_SPECIFIED;
437
438 pj_list_init(&hdr_list);
439 msg = rdata->msg_info.msg;
440 h = (pjsip_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_EXPIRES, NULL);
441 if (h) {
442 expires = ((pjsip_expires_hdr*)h)->ivalue;
443 pj_list_push_back(&hdr_list, pjsip_hdr_clone(rdata->tp_info.pool, h));
444 PJ_LOG(3,(THIS_FILE, " Expires=%d", expires));
445 }
446 if (expires != 0 && expires != PJSIP_EXPIRES_NOT_SPECIFIED) {
447 h = (pjsip_hdr*)pjsip_msg_find_hdr(msg, PJSIP_H_CONTACT, NULL);
448 if (h)
449 pj_list_push_back(&hdr_list, pjsip_hdr_clone(rdata->tp_info.pool, h));
450 }
451
452 pjsip_endpt_respond(app.sip_endpt, &mod_sipecho, rdata, 200, NULL,
453 &hdr_list, NULL, NULL);
454 return PJ_TRUE;
455 }
456
457 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
458 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
459 pj_str_t reason = pj_str("Go away");
460 pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
461 400, &reason,
462 NULL, NULL);
463 }
464 return PJ_TRUE;
465 }
466
467 sdp_info = pjsip_rdata_get_sdp_info(rdata);
468 if (!sdp_info || !sdp_info->sdp) {
469 pj_str_t reason = pj_str("Require valid offer");
470 pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
471 400, &reason,
472 NULL, NULL);
473 return PJ_TRUE;
474 }
475
476 for (i=0; i<MAX_CALLS; ++i) {
477 if (app.call[i].inv == NULL) {
478 call = &app.call[i];
479 break;
480 }
481 }
482
483 if (i==MAX_CALLS) {
484 pj_str_t reason = pj_str("We're full");
485 pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
486 PJSIP_SC_BUSY_HERE, &reason,
487 NULL, NULL);
488 return PJ_TRUE;
489 }
490
491 /* Generate Contact URI */
492 status = pj_gethostip(sip_af, &hostaddr);
493 if (status != PJ_SUCCESS) {
494 app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
495 return PJ_TRUE;
496 }
497 pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
498 pj_ansi_snprintf(temp, sizeof(temp), "<sip:sipecho@%s:%d>",
499 hostip, sip_port);
500 local_uri = pj_str(temp);
501
502 status = pjsip_dlg_create_uas_and_inc_lock( pjsip_ua_instance(), rdata,
503 &local_uri, &dlg);
504
505 if (status == PJ_SUCCESS)
506 answer = create_answer((int)(call-app.call), dlg->pool, sdp_info->sdp);
507
508 if (status == PJ_SUCCESS)
509 status = pjsip_inv_create_uas( dlg, rdata, answer, 0, &call->inv);
510
511 if (dlg)
512 pjsip_dlg_dec_lock(dlg);
513
514 if (status == PJ_SUCCESS)
515 status = pjsip_inv_initial_answer(call->inv, rdata, 100,
516 NULL, NULL, &tdata);
517 if (status == PJ_SUCCESS)
518 status = pjsip_inv_send_msg(call->inv, tdata);
519
520 if (status == PJ_SUCCESS)
521 status = pjsip_inv_answer(call->inv, 180, NULL,
522 NULL, &tdata);
523 if (status == PJ_SUCCESS)
524 status = pjsip_inv_send_msg(call->inv, tdata);
525
526 if (status == PJ_SUCCESS)
527 status = pjsip_inv_answer(call->inv, 200, NULL,
528 NULL, &tdata);
529 if (status == PJ_SUCCESS)
530 status = pjsip_inv_send_msg(call->inv, tdata);
531
532 if (status != PJ_SUCCESS) {
533 pjsip_transaction *tsx;
534
535 app_perror(THIS_FILE, "Unable to create/send 1xx/200 response", status);
536
537 tsx = pjsip_rdata_get_tsx(rdata);
538 if (tsx == NULL || tsx->state >= PJSIP_TSX_STATE_TERMINATED) {
539 /* Respond statelessly if tsx is stateless or already terminated */
540 status = pjsip_endpt_respond_stateless( app.sip_endpt, rdata,
541 500, NULL, NULL, NULL);
542 } else {
543 /* Otherwise, respond statefully */
544 status = pjsip_endpt_create_response( app.sip_endpt, rdata,
545 500, NULL, &tdata);
546 if (status == PJ_SUCCESS)
547 status = pjsip_tsx_send_msg(tsx, tdata);
548 }
549 if (status != PJ_SUCCESS) {
550 app_perror(THIS_FILE, "Unable to create/send 500 response",
551 status);
552 }
553 destroy_call(call);
554 } else {
555 call->inv->mod_data[mod_sipecho.id] = call;
556 }
557
558 return PJ_TRUE;
559 }
560
561 static void call_on_media_update( pjsip_inv_session *inv,
562 pj_status_t status)
563 {
564 PJ_UNUSED_ARG(inv);
565 PJ_UNUSED_ARG(status);
566 }
567
568
569 static void usage()
570 {
571 printf("\nUsage: sipecho OPTIONS\n");
572 printf("\n");
573 printf("where OPTIONS:\n");
574 printf(" --local-port, -p PORT Bind to port PORT.\n");
575 printf(" --tcp, -t Listen to TCP instead.\n");
576 printf(" --ipv6, -6 Use IPv6 instead.\n");
577 printf(" --help, -h Show this help page.\n");
578 }
579
580 /* main()
581 *
582 * If called with argument, treat argument as SIP URL to be called.
583 * Otherwise wait for incoming calls.
584 */
585 int main(int argc, char *argv[])
586 {
587 struct pj_getopt_option long_options[] = {
588 { "local-port", 1, 0, 'p' },
589 { "tcp", 0, 0, 't' },
590 { "ipv6", 0, 0, '6' },
591 { "help", 0, 0, 'h' }
592 };
593 int c, option_index;
594 pj_status_t status;
595
596
597 pj_log_set_level(5);
598
599 pj_init();
600
601 sip_af = pj_AF_INET();
602
603 pj_optind = 0;
604 while ((c = pj_getopt_long(argc, argv, "p:t6h", long_options,
605 &option_index)) != -1)
606 {
607 switch (c) {
608 case 'p':
609 sip_port = atoi(pj_optarg);
610 break;
611 case 't':
612 sip_tcp = PJ_TRUE;
613 break;
614 case 'h':
615 usage();
616 return 0;
617 case '6':
618 sip_af = pj_AF_INET6();
619 break;
620 default:
621 PJ_LOG(1,(THIS_FILE,
622 "Argument \"%s\" is not valid. Use --help to see help",
623 argv[pj_optind-1]));
624 return -1;
625 }
626 }
627
628 status = init_stack();
629 if (status != PJ_SUCCESS)
630 goto on_error;
631
632 /* If URL is specified, then make call immediately. */
633 if (pj_optind != argc) {
634 pj_sockaddr hostaddr;
635 char hostip[PJ_INET6_ADDRSTRLEN+2];
636 char temp[80];
637 call_t *call;
638 pj_str_t dst_uri = pj_str(argv[pj_optind]);
639 pj_str_t local_uri;
640 pjsip_dialog *dlg;
641 pjsip_tx_data *tdata;
642
643 CHECK( pj_gethostip(sip_af, &hostaddr) );
644 pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
645
646 pj_ansi_snprintf(temp, sizeof(temp), "<sip:sipecho@%s:%d>",
647 hostip, sip_port);
648 local_uri = pj_str(temp);
649
650 call = &app.call[0];
651
652 CHECK( pjsip_dlg_create_uac( pjsip_ua_instance(),
653 &local_uri, /* local URI */
654 &local_uri, /* local Contact */
655 &dst_uri, /* remote URI */
656 &dst_uri, /* remote target */
657 &dlg) ); /* dialog */
658
659 CHECK( pjsip_inv_create_uac( dlg, NULL, 0, &call->inv) );
660
661 call->inv->mod_data[mod_sipecho.id] = call;
662
663 CHECK( pjsip_inv_invite(call->inv, &tdata) );
664 if (status != PJ_SUCCESS) goto on_error;
665
666 CHECK( pjsip_inv_send_msg(call->inv, tdata) );
667 if (status != PJ_SUCCESS) goto on_error;
668
669 puts("Press ENTER to quit...");
670 } else {
671 puts("Ready for incoming calls. Press ENTER to quit...");
672 }
673
674 for (;;) {
675 char s[10];
676
677 printf("\nMenu:\n"
678 " h Hangup all calls\n"
679 " l %s message logging\n"
680 " q Quit\n",
681 (app.enable_msg_logging? "Disable" : "Enable"));
682
683 if (fgets(s, sizeof(s), stdin) == NULL)
684 continue;
685
686 if (s[0]=='q')
687 break;
688 switch (s[0]) {
689 case 'l':
690 app.enable_msg_logging = !app.enable_msg_logging;
691 break;
692 case 'h':
693 hangup_all();
694 break;
695 }
696 }
697
698 destroy_stack();
699
700 puts("Bye bye..");
701 return 0;
702
703 on_error:
704 return 1;
705 }
706