blob: b09a7c59d58745eef2213d384b3c5bf8045dfb06 [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 Prijono268ca612006-02-07 12:34:11 +000020
Benny Prijono84126ab2006-02-09 09:30:09 +000021/*
22 * pjsua_core.c
23 *
24 * Core application functionalities.
25 */
Benny Prijono268ca612006-02-07 12:34:11 +000026
Benny Prijono84126ab2006-02-09 09:30:09 +000027#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000028
29
Benny Prijono95196582006-02-09 00:13:40 +000030/*
Benny Prijono84126ab2006-02-09 09:30:09 +000031 * Global variable.
32 */
33struct pjsua pjsua;
34
35
36/*
37 * Default local URI, if none is specified in cmd-line
Benny Prijono95196582006-02-09 00:13:40 +000038 */
Benny Prijonoccf95622006-02-07 18:48:01 +000039#define PJSUA_LOCAL_URI "<sip:user@127.0.0.1>"
Benny Prijono268ca612006-02-07 12:34:11 +000040
Benny Prijono268ca612006-02-07 12:34:11 +000041
Benny Prijono268ca612006-02-07 12:34:11 +000042
43/*
44 * Init default application parameters.
45 */
46void pjsua_default(void)
47{
Benny Prijonoa91a0032006-02-26 21:23:45 +000048 unsigned i;
49
Benny Prijono268ca612006-02-07 12:34:11 +000050
51 /* Normally need another thread for console application, because main
52 * thread will be blocked in fgets().
53 */
54 pjsua.thread_cnt = 1;
55
56
57 /* Default transport settings: */
Benny Prijono268ca612006-02-07 12:34:11 +000058 pjsua.sip_port = 5060;
59
60
61 /* Default logging settings: */
Benny Prijono268ca612006-02-07 12:34:11 +000062 pjsua.log_level = 5;
63 pjsua.app_log_level = 4;
64 pjsua.log_decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
65 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
66
Benny Prijono268ca612006-02-07 12:34:11 +000067
Benny Prijonoa91a0032006-02-26 21:23:45 +000068 /* Default: do not use STUN: */
Benny Prijono268ca612006-02-07 12:34:11 +000069 pjsua.stun_port1 = pjsua.stun_port2 = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +000070
Benny Prijonoa91a0032006-02-26 21:23:45 +000071 /* Init accounts: */
72 pjsua.acc_cnt = 1;
73 for (i=0; i<PJ_ARRAY_SIZE(pjsua.acc); ++i) {
74 pjsua.acc[i].index = i;
75 pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI);
76 pjsua.acc[i].reg_timeout = 55;
77 pj_list_init(&pjsua.acc[i].route_set);
78 pj_list_init(&pjsua.acc[i].pres_srv_list);
79 }
Benny Prijonoccf95622006-02-07 18:48:01 +000080
Benny Prijonoa91a0032006-02-26 21:23:45 +000081 /* Init call array: */
82 for (i=0; i<PJ_ARRAY_SIZE(pjsua.calls); ++i)
83 pjsua.calls[i].index = i;
Benny Prijono95196582006-02-09 00:13:40 +000084
Benny Prijonoa91a0032006-02-26 21:23:45 +000085 /* Default max nb of calls. */
86 pjsua.max_calls = 4;
Benny Prijono834aee32006-02-19 01:38:06 +000087
88 /* Init server presence subscription list: */
89
Benny Prijono834aee32006-02-19 01:38:06 +000090
Benny Prijono268ca612006-02-07 12:34:11 +000091}
92
93
Benny Prijono268ca612006-02-07 12:34:11 +000094
95/*
96 * Handler for receiving incoming requests.
97 *
98 * This handler serves multiple purposes:
99 * - it receives requests outside dialogs.
100 * - it receives requests inside dialogs, when the requests are
101 * unhandled by other dialog usages. Example of these
102 * requests are: MESSAGE.
103 */
104static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
105{
Benny Prijono38998232006-02-08 22:44:25 +0000106
Benny Prijono84126ab2006-02-09 09:30:09 +0000107 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000108
Benny Prijonoa91a0032006-02-26 21:23:45 +0000109 return pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000110 }
111
Benny Prijono268ca612006-02-07 12:34:11 +0000112 return PJ_FALSE;
113}
114
115
116/*
117 * Handler for receiving incoming responses.
118 *
119 * This handler serves multiple purposes:
120 * - it receives strayed responses (i.e. outside any dialog and
121 * outside any transactions).
122 * - it receives responses coming to a transaction, when pjsua
123 * module is set as transaction user for the transaction.
124 * - it receives responses inside a dialog, when these responses
125 * are unhandled by other dialog usages.
126 */
127static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
128{
129 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000130 return PJ_FALSE;
131}
132
133
Benny Prijono268ca612006-02-07 12:34:11 +0000134/*
135 * Initialize sockets and optionally get the public address via STUN.
136 */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000137static pj_status_t init_sockets(pj_bool_t sip,
138 pjmedia_sock_info *skinfo)
Benny Prijono268ca612006-02-07 12:34:11 +0000139{
140 enum {
141 RTP_START_PORT = 4000,
142 RTP_RANDOM_START = 2,
Benny Prijonoa91a0032006-02-26 21:23:45 +0000143 RTP_RETRY = 100
Benny Prijono268ca612006-02-07 12:34:11 +0000144 };
145 enum {
146 SIP_SOCK,
147 RTP_SOCK,
148 RTCP_SOCK,
149 };
150 int i;
Benny Prijonofccab712006-02-22 22:23:22 +0000151 static pj_uint16_t rtp_port = RTP_START_PORT;
Benny Prijono268ca612006-02-07 12:34:11 +0000152 pj_sock_t sock[3];
153 pj_sockaddr_in mapped_addr[3];
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000154 pj_status_t status = PJ_SUCCESS;
Benny Prijono268ca612006-02-07 12:34:11 +0000155
156 for (i=0; i<3; ++i)
157 sock[i] = PJ_INVALID_SOCKET;
158
Benny Prijonofccab712006-02-22 22:23:22 +0000159 /* Create and bind SIP UDP socket. */
160 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
161 if (status != PJ_SUCCESS) {
162 pjsua_perror(THIS_FILE, "socket() error", status);
163 goto on_error;
164 }
165
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000166 if (sip) {
Benny Prijonofccab712006-02-22 22:23:22 +0000167 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000168 if (status != PJ_SUCCESS) {
Benny Prijonofccab712006-02-22 22:23:22 +0000169 pjsua_perror(THIS_FILE, "bind() error", status);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000170 goto on_error;
171 }
Benny Prijonofccab712006-02-22 22:23:22 +0000172 } else {
173 status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000174 if (status != PJ_SUCCESS) {
175 pjsua_perror(THIS_FILE, "bind() error", status);
176 goto on_error;
177 }
Benny Prijono268ca612006-02-07 12:34:11 +0000178 }
179
Benny Prijono268ca612006-02-07 12:34:11 +0000180
181 /* Loop retry to bind RTP and RTCP sockets. */
182 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
183
184 /* Create and bind RTP socket. */
185 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
186 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000187 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000188 goto on_error;
189 }
190
191 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
192 if (status != PJ_SUCCESS) {
193 pj_sock_close(sock[RTP_SOCK]);
194 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
195 continue;
196 }
197
198 /* Create and bind RTCP socket. */
199 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
200 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000201 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000202 goto on_error;
203 }
204
205 status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
206 if (status != PJ_SUCCESS) {
207 pj_sock_close(sock[RTP_SOCK]);
208 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
209
210 pj_sock_close(sock[RTCP_SOCK]);
211 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
212 continue;
213 }
214
215 /*
216 * If we're configured to use STUN, then find out the mapped address,
217 * and make sure that the mapped RTCP port is adjacent with the RTP.
218 */
219 if (pjsua.stun_port1 == 0) {
220 const pj_str_t *hostname;
221 pj_sockaddr_in addr;
222
223 /* Get local IP address. */
224 hostname = pj_gethostname();
225
226 pj_memset( &addr, 0, sizeof(addr));
227 addr.sin_family = PJ_AF_INET;
228 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
229 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000230 pjsua_perror(THIS_FILE, "Unresolvable local hostname",
231 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000232 goto on_error;
233 }
234
235 for (i=0; i<3; ++i)
236 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
237
Benny Prijonoa91a0032006-02-26 21:23:45 +0000238 if (sip) {
239 mapped_addr[SIP_SOCK].sin_port =
240 pj_htons((pj_uint16_t)pjsua.sip_port);
241 }
242 mapped_addr[RTP_SOCK].sin_port=pj_htons((pj_uint16_t)rtp_port);
243 mapped_addr[RTCP_SOCK].sin_port=pj_htons((pj_uint16_t)(rtp_port+1));
Benny Prijono268ca612006-02-07 12:34:11 +0000244 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000245
Benny Prijono268ca612006-02-07 12:34:11 +0000246 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000247 status=pj_stun_get_mapped_addr(&pjsua.cp.factory, 3, sock,
248 &pjsua.stun_srv1, pjsua.stun_port1,
249 &pjsua.stun_srv2, pjsua.stun_port2,
250 mapped_addr);
Benny Prijono268ca612006-02-07 12:34:11 +0000251 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000252 pjsua_perror(THIS_FILE, "STUN error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000253 goto on_error;
254 }
255
Benny Prijonoa91a0032006-02-26 21:23:45 +0000256 if (pj_ntohs(mapped_addr[2].sin_port) ==
257 pj_ntohs(mapped_addr[1].sin_port)+1)
258 {
Benny Prijono268ca612006-02-07 12:34:11 +0000259 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000260 }
Benny Prijono268ca612006-02-07 12:34:11 +0000261
Benny Prijonoa91a0032006-02-26 21:23:45 +0000262 pj_sock_close(sock[RTP_SOCK]);
263 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
264
265 pj_sock_close(sock[RTCP_SOCK]);
266 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
Benny Prijono268ca612006-02-07 12:34:11 +0000267 }
268 }
269
270 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000271 PJ_LOG(1,(THIS_FILE,
272 "Unable to find appropriate RTP/RTCP ports combination"));
Benny Prijono268ca612006-02-07 12:34:11 +0000273 goto on_error;
274 }
275
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000276 if (sip) {
277 pjsua.sip_sock = sock[SIP_SOCK];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000278 pj_memcpy(&pjsua.sip_sock_name,
279 &mapped_addr[SIP_SOCK],
280 sizeof(pj_sockaddr_in));
Benny Prijonofccab712006-02-22 22:23:22 +0000281 } else {
282 pj_sock_close(sock[0]);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000283 }
Benny Prijono95196582006-02-09 00:13:40 +0000284
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000285 skinfo->rtp_sock = sock[RTP_SOCK];
286 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000287 &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
288
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000289 skinfo->rtcp_sock = sock[RTCP_SOCK];
290 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000291 &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijono268ca612006-02-07 12:34:11 +0000292
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000293 if (sip) {
294 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
295 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
296 pj_ntohs(pjsua.sip_sock_name.sin_port)));
297 }
Benny Prijono268ca612006-02-07 12:34:11 +0000298 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000299 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
300 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000301 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000302 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
303 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000304
Benny Prijonofccab712006-02-22 22:23:22 +0000305 rtp_port += 2;
Benny Prijono268ca612006-02-07 12:34:11 +0000306 return PJ_SUCCESS;
307
308on_error:
309 for (i=0; i<3; ++i) {
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000310 if (sip && i==0)
311 continue;
Benny Prijono268ca612006-02-07 12:34:11 +0000312 if (sock[i] != PJ_INVALID_SOCKET)
313 pj_sock_close(sock[i]);
314 }
315 return status;
316}
317
318
319
320/*
321 * Initialize stack.
322 */
323static pj_status_t init_stack(void)
324{
325 pj_status_t status;
326
327 /* Create global endpoint: */
328
329 {
330 const pj_str_t *hostname;
331 const char *endpt_name;
332
333 /* Endpoint MUST be assigned a globally unique name.
334 * The name will be used as the hostname in Warning header.
335 */
336
337 /* For this implementation, we'll use hostname for simplicity */
338 hostname = pj_gethostname();
339 endpt_name = hostname->ptr;
340
341 /* Create the endpoint: */
342
343 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
344 &pjsua.endpt);
345 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000346 pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000347 return status;
348 }
349 }
350
351
352 /* Initialize transaction layer: */
353
Benny Prijono2f8992b2006-02-25 21:16:36 +0000354 status = pjsip_tsx_layer_init_module(pjsua.endpt);
Benny Prijono268ca612006-02-07 12:34:11 +0000355 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000356 pjsua_perror(THIS_FILE, "Transaction layer initialization error",
357 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000358 goto on_error;
359 }
360
361 /* Initialize UA layer module: */
362
Benny Prijono2f8992b2006-02-25 21:16:36 +0000363 status = pjsip_ua_init_module( pjsua.endpt, NULL );
Benny Prijono268ca612006-02-07 12:34:11 +0000364 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000365 pjsua_perror(THIS_FILE, "UA layer initialization error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000366 goto on_error;
367 }
368
369 /* Initialize and register pjsua's application module: */
370
371 {
372 pjsip_module my_mod =
373 {
374 NULL, NULL, /* prev, next. */
375 { "mod-pjsua", 9 }, /* Name. */
376 -1, /* Id */
377 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono268ca612006-02-07 12:34:11 +0000378 NULL, /* load() */
379 NULL, /* start() */
380 NULL, /* stop() */
381 NULL, /* unload() */
382 &mod_pjsua_on_rx_request, /* on_rx_request() */
383 &mod_pjsua_on_rx_response, /* on_rx_response() */
384 NULL, /* on_tx_request. */
385 NULL, /* on_tx_response() */
386 NULL, /* on_tsx_state() */
387 };
388
389 pjsua.mod = my_mod;
390
391 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
392 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000393 pjsua_perror(THIS_FILE, "Unable to register pjsua module",
394 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000395 goto on_error;
396 }
397 }
398
399 /* Initialize invite session module: */
400
Benny Prijonoa91a0032006-02-26 21:23:45 +0000401 status = pjsua_call_init();
402 if (status != PJ_SUCCESS) {
403 pjsua_perror(THIS_FILE, "Invite usage initialization error",
404 status);
405 goto on_error;
Benny Prijono268ca612006-02-07 12:34:11 +0000406 }
407
Benny Prijonoccf95622006-02-07 18:48:01 +0000408 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000409
410 return PJ_SUCCESS;
411
412
413on_error:
414 pjsip_endpt_destroy(pjsua.endpt);
415 pjsua.endpt = NULL;
416 return status;
417}
418
419
Benny Prijono834aee32006-02-19 01:38:06 +0000420static int PJ_THREAD_FUNC pjsua_poll(void *arg)
Benny Prijono268ca612006-02-07 12:34:11 +0000421{
422 PJ_UNUSED_ARG(arg);
423
Benny Prijono834aee32006-02-19 01:38:06 +0000424 do {
Benny Prijono268ca612006-02-07 12:34:11 +0000425 pj_time_val timeout = { 0, 10 };
426 pjsip_endpt_handle_events (pjsua.endpt, &timeout);
Benny Prijono834aee32006-02-19 01:38:06 +0000427 } while (!pjsua.quit_flag);
Benny Prijono268ca612006-02-07 12:34:11 +0000428
429 return 0;
430}
431
432/*
433 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000434 * This will initialize all libraries, create endpoint instance, and register
435 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000436 */
437pj_status_t pjsua_init(void)
438{
Benny Prijono268ca612006-02-07 12:34:11 +0000439 pj_status_t status;
440
441 /* Init PJLIB logging: */
442
443 pj_log_set_level(pjsua.log_level);
444 pj_log_set_decor(pjsua.log_decor);
445
446
447 /* Init PJLIB: */
448
449 status = pj_init();
450 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000451 pjsua_perror(THIS_FILE, "pj_init() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000452 return status;
453 }
454
Benny Prijonofccab712006-02-22 22:23:22 +0000455 /* Init PJLIB-UTIL: */
456
457 status = pjlib_util_init();
458 if (status != PJ_SUCCESS) {
459 pjsua_perror(THIS_FILE, "pjlib_util_init() error", status);
460 return status;
461 }
462
Benny Prijono268ca612006-02-07 12:34:11 +0000463 /* Init memory pool: */
464
465 /* Init caching pool. */
466 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
467
468 /* Create memory pool for application. */
469 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
470
471
Benny Prijono834aee32006-02-19 01:38:06 +0000472 /* Init PJSIP : */
Benny Prijonoccf95622006-02-07 18:48:01 +0000473
474 status = init_stack();
475 if (status != PJ_SUCCESS) {
476 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000477 pjsua_perror(THIS_FILE, "Stack initialization has returned error",
478 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000479 return status;
480 }
481
Benny Prijono95196582006-02-09 00:13:40 +0000482
Benny Prijono834aee32006-02-19 01:38:06 +0000483 /* Init core SIMPLE module : */
484
485 pjsip_evsub_init_module(pjsua.endpt);
486
487 /* Init presence module: */
488
489 pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance());
490
Benny Prijono26ff9062006-02-21 23:47:00 +0000491 /* Init xfer/REFER module */
492
493 pjsip_xfer_init_module( pjsua.endpt );
Benny Prijono834aee32006-02-19 01:38:06 +0000494
495 /* Init pjsua presence handler: */
496
497 pjsua_pres_init();
498
499
Benny Prijono95196582006-02-09 00:13:40 +0000500 /* Init media endpoint: */
501
502 status = pjmedia_endpt_create(&pjsua.cp.factory, &pjsua.med_endpt);
503 if (status != PJ_SUCCESS) {
504 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000505 pjsua_perror(THIS_FILE,
506 "Media stack initialization has returned error",
507 status);
Benny Prijono95196582006-02-09 00:13:40 +0000508 return status;
509 }
510
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000511 /* Init conference bridge. */
512
Benny Prijonoa91a0032006-02-26 21:23:45 +0000513 status = pjmedia_conf_create(pjsua.pool,
514 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijono39879152006-02-23 02:09:10 +0000515 8000, 160, 16, &pjsua.mconf);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000516 if (status != PJ_SUCCESS) {
517 pj_caching_pool_destroy(&pjsua.cp);
518 pjsua_perror(THIS_FILE,
519 "Media stack initialization has returned error",
520 status);
521 return status;
522 }
523
Benny Prijono1f9afba2006-02-10 15:57:32 +0000524 /* Init pjmedia-codecs: */
525
526 status = pjmedia_codec_init(pjsua.med_endpt);
527 if (status != PJ_SUCCESS) {
528 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000529 pjsua_perror(THIS_FILE,
530 "Media codec initialization has returned error",
531 status);
Benny Prijono1f9afba2006-02-10 15:57:32 +0000532 return status;
533 }
534
535
Benny Prijonoccf95622006-02-07 18:48:01 +0000536 /* Done. */
537 return PJ_SUCCESS;
538}
539
540
Benny Prijonoa91a0032006-02-26 21:23:45 +0000541/*
542 * Find account for incoming request.
543 */
544int pjsua_find_account_for_incoming(pjsip_rx_data *rdata)
545{
546 pjsip_uri *uri;
547 pjsip_sip_uri *sip_uri;
548 int acc_index;
549
550 uri = rdata->msg_info.to->uri;
551
552 /* Just return account #0 if To URI is not SIP: */
553 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
554 !PJSIP_URI_SCHEME_IS_SIPS(uri))
555 {
556 return 0;
557 }
558
559
560 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
561
562 /* Find account which has matching username and domain. */
563 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
564
565 pjsua_acc *acc = &pjsua.acc[acc_index];
566
567 if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
568 pj_stricmp(&acc->host_part, &sip_uri->host)==0)
569 {
570 /* Match ! */
571 return acc_index;
572 }
573 }
574
575 /* No matching, try match domain part only. */
576 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
577
578 pjsua_acc *acc = &pjsua.acc[acc_index];
579
580 if (pj_stricmp(&acc->host_part, &sip_uri->host)==0) {
581 /* Match ! */
582 return acc_index;
583 }
584 }
585
586 /* Still no match, just return account #0 */
587 return 0;
588}
589
590
591/*
592 * Find account for outgoing request.
593 */
594int pjsua_find_account_for_outgoing(const pj_str_t *url)
595{
596 PJ_UNUSED_ARG(url);
597
598 /* Just use account #0 */
599 return 0;
600}
601
Benny Prijonoccf95622006-02-07 18:48:01 +0000602
603/*
604 * Start pjsua stack.
605 * This will start the registration process, if registration is configured.
606 */
607pj_status_t pjsua_start(void)
608{
609 int i; /* Must be signed */
610 pjsip_transport *udp_transport;
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000611 pj_status_t status = PJ_SUCCESS;
Benny Prijonoccf95622006-02-07 18:48:01 +0000612
Benny Prijono39879152006-02-23 02:09:10 +0000613 /* Create WAV file player if required: */
614
615 if (pjsua.wav_file) {
616 pjmedia_port *port;
617 pj_str_t port_name;
618
619 /* Create the file player port. */
620 status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file,
621 0, -1, NULL, &port);
622 if (status != PJ_SUCCESS) {
623 pjsua_perror(THIS_FILE,
624 "Error playing media file",
625 status);
626 return status;
627 }
628
629 /* Add port to conference bridge: */
630 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool, port,
631 pj_cstr(&port_name, pjsua.wav_file),
632 &pjsua.wav_slot);
633 if (status != PJ_SUCCESS) {
634 pjsua_perror(THIS_FILE,
635 "Unable to add file player to conference bridge",
636 status);
637 return status;
638 }
639 }
640
641
Benny Prijono268ca612006-02-07 12:34:11 +0000642 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000643 for (i=0; i<(int)pjsua.max_calls; ++i) {
644 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000645 if (status != PJ_SUCCESS) {
646 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
647 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000648 --i;
649 if (i >= 0)
650 pj_sock_close(pjsua.sip_sock);
651 while (i >= 0) {
652 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
653 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
654 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000655 return status;
656 }
Benny Prijono268ca612006-02-07 12:34:11 +0000657 }
658
Benny Prijonoccf95622006-02-07 18:48:01 +0000659 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000660
Benny Prijonoccf95622006-02-07 18:48:01 +0000661 {
662 /* Init the published name for the transport.
663 * Depending whether STUN is used, this may be the STUN mapped
664 * address, or socket's bound address.
665 */
666 pjsip_host_port addr_name;
667
668 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000669 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000670 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
671
672 /* Create UDP transport from previously created UDP socket: */
673
674 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
675 &addr_name, 1,
676 &udp_transport);
677 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000678 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
679 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000680 return status;
681 }
Benny Prijono268ca612006-02-07 12:34:11 +0000682 }
683
Benny Prijonoccf95622006-02-07 18:48:01 +0000684 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000685 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000686
687 pjsip_uri *uri;
688 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000689
Benny Prijonoccf95622006-02-07 18:48:01 +0000690 /* Need to parse local_uri to get the elements: */
691
Benny Prijonoa91a0032006-02-26 21:23:45 +0000692 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
693 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000694 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000695 pjsua_perror(THIS_FILE, "Invalid local URI",
696 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000697 return PJSIP_EINVALIDURI;
698 }
699
Benny Prijonoccf95622006-02-07 18:48:01 +0000700 /* Local URI MUST be a SIP or SIPS: */
701
Benny Prijonoa91a0032006-02-26 21:23:45 +0000702 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
703 !PJSIP_URI_SCHEME_IS_SIPS(uri))
704 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000705 pjsua_perror(THIS_FILE, "Invalid local URI",
706 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000707 return PJSIP_EINVALIDSCHEME;
708 }
709
710
711 /* Get the SIP URI object: */
712
713 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
714
Benny Prijonoa91a0032006-02-26 21:23:45 +0000715 pjsua.acc[i].user_part = sip_uri->user;
716 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000717
Benny Prijonoa91a0032006-02-26 21:23:45 +0000718 if (pjsua.acc[i].contact_uri.slen == 0 &&
719 pjsua.acc[i].local_uri.slen)
720 {
721 char contact[128];
722 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000723
Benny Prijonoa91a0032006-02-26 21:23:45 +0000724 /* The local Contact is the username@ip-addr, where
725 * - username is taken from the local URI,
726 * - ip-addr in UDP transport's address name (which may have been
727 * resolved from STUN.
728 */
729
730 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000731
Benny Prijonoa91a0032006-02-26 21:23:45 +0000732 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000733
Benny Prijonoa91a0032006-02-26 21:23:45 +0000734 /* With the user part. */
735 len = pj_snprintf(contact, sizeof(contact),
736 "<sip:%.*s@%.*s:%d>",
737 (int)sip_uri->user.slen,
738 sip_uri->user.ptr,
739 (int)udp_transport->local_name.host.slen,
740 udp_transport->local_name.host.ptr,
741 udp_transport->local_name.port);
742 } else {
743
744 /* Without user part */
745
746 len = pj_snprintf(contact, sizeof(contact),
747 "<sip:%.*s:%d>",
748 (int)udp_transport->local_name.host.slen,
749 udp_transport->local_name.host.ptr,
750 udp_transport->local_name.port);
751 }
752
753 if (len < 1 || len >= sizeof(contact)) {
754 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
755 return PJSIP_EURITOOLONG;
756 }
757
758 /* Duplicate Contact uri. */
759
760 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
761
Benny Prijonoccf95622006-02-07 18:48:01 +0000762 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000763 }
764
Benny Prijono95196582006-02-09 00:13:40 +0000765 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000766
Benny Prijono95196582006-02-09 00:13:40 +0000767 if (pjsua.outbound_proxy.slen) {
768
769 pjsip_route_hdr *route;
770 const pj_str_t hname = { "Route", 5 };
771 int parsed_len;
772
773 route = pjsip_parse_hdr( pjsua.pool, &hname,
774 pjsua.outbound_proxy.ptr,
775 pjsua.outbound_proxy.slen,
776 &parsed_len);
777 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000778 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
779 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000780 return PJSIP_EINVALIDURI;
781 }
782
Benny Prijonoa91a0032006-02-26 21:23:45 +0000783 for (i=0; i<pjsua.acc_cnt; ++i) {
784 pj_list_push_front(&pjsua.acc[i].route_set, route);
785 }
Benny Prijono95196582006-02-09 00:13:40 +0000786 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000787
788
Benny Prijono268ca612006-02-07 12:34:11 +0000789 /* Create worker thread(s), if required: */
790
791 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000792 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000793 NULL, 0, 0, &pjsua.threads[i]);
794 if (status != PJ_SUCCESS) {
795 pjsua.quit_flag = 1;
796 for (--i; i>=0; --i) {
797 pj_thread_join(pjsua.threads[i]);
798 pj_thread_destroy(pjsua.threads[i]);
799 }
Benny Prijono268ca612006-02-07 12:34:11 +0000800 return status;
801 }
802 }
803
Benny Prijonoccf95622006-02-07 18:48:01 +0000804 /* Start registration: */
805
806 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000807 for (i=0; i<pjsua.acc_cnt; ++i) {
808 status = pjsua_regc_init(i);
809 if (status != PJ_SUCCESS)
810 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +0000811
Benny Prijonoa91a0032006-02-26 21:23:45 +0000812 /* Perform registration, if required. */
813 if (pjsua.acc[i].regc) {
814 pjsua_regc_update(i, 1);
815 }
816 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000817
Benny Prijonoa91a0032006-02-26 21:23:45 +0000818
819 /* Find account for outgoing preence subscription */
820 for (i=0; i<pjsua.buddy_cnt; ++i) {
821 pjsua.buddies[i].acc_index =
822 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +0000823 }
824
825
Benny Prijono834aee32006-02-19 01:38:06 +0000826 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000827 return PJ_SUCCESS;
828}
829
830
Benny Prijono834aee32006-02-19 01:38:06 +0000831/* Sleep with polling */
832static void busy_sleep(unsigned msec)
833{
834 pj_time_val timeout, now;
835
836 pj_gettimeofday(&timeout);
837 timeout.msec += msec;
838 pj_time_val_normalize(&timeout);
839
840 do {
841 pjsua_poll(NULL);
842 pj_gettimeofday(&now);
843 } while (PJ_TIME_VAL_LT(now, timeout));
844}
845
Benny Prijono268ca612006-02-07 12:34:11 +0000846/*
847 * Destroy pjsua.
848 */
849pj_status_t pjsua_destroy(void)
850{
851 int i;
852
853 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +0000854 pjsua.quit_flag = 1;
855
Benny Prijonoa91a0032006-02-26 21:23:45 +0000856 /* Terminate all calls. */
857 pjsua_inv_shutdown();
858
859 /* Terminate all presence subscriptions. */
860 pjsua_pres_shutdown();
861
862 /* Unregister, if required: */
863 for (i=0; i<pjsua.acc_cnt; ++i) {
864 if (pjsua.acc[i].regc) {
865 pjsua_regc_update(i, 0);
866 }
867 }
868
Benny Prijono268ca612006-02-07 12:34:11 +0000869 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +0000870 for (i=0; i<pjsua.thread_cnt; ++i) {
871
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000872 if (pjsua.threads[i]) {
873 pj_thread_join(pjsua.threads[i]);
874 pj_thread_destroy(pjsua.threads[i]);
875 pjsua.threads[i] = NULL;
876 }
Benny Prijono268ca612006-02-07 12:34:11 +0000877 }
878
Benny Prijono834aee32006-02-19 01:38:06 +0000879
Benny Prijono834aee32006-02-19 01:38:06 +0000880 /* Wait for some time to allow unregistration to complete: */
881 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
882 busy_sleep(1000);
883
Benny Prijono26ff9062006-02-21 23:47:00 +0000884 /* Destroy conference bridge. */
885 if (pjsua.mconf)
886 pjmedia_conf_destroy(pjsua.mconf);
887
Benny Prijono834aee32006-02-19 01:38:06 +0000888 /* Shutdown pjmedia-codec: */
889 pjmedia_codec_deinit();
890
891 /* Destroy sound framework:
892 * (this should be done in pjmedia_shutdown())
893 */
894 pj_snd_deinit();
895
Benny Prijono268ca612006-02-07 12:34:11 +0000896 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +0000897
Benny Prijono268ca612006-02-07 12:34:11 +0000898 pjsip_endpt_destroy(pjsua.endpt);
899 pjsua.endpt = NULL;
900
901 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +0000902
Benny Prijono268ca612006-02-07 12:34:11 +0000903 pj_caching_pool_destroy(&pjsua.cp);
904
905
906 /* Done. */
907
908 return PJ_SUCCESS;
909}
910