From 813cd5ef6791ddf9778ca61c08ce19f48202b3bc Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Tue, 7 Feb 2006 18:48:01 +0000 Subject: Tested initial implementation: basic UAC, client registration, authentication, etc git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@141 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip/src/pjsua/pjsua.c | 216 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 163 insertions(+), 53 deletions(-) (limited to 'pjsip/src/pjsua/pjsua.c') diff --git a/pjsip/src/pjsua/pjsua.c b/pjsip/src/pjsua/pjsua.c index 8e9cbde6..a5fafe66 100644 --- a/pjsip/src/pjsua/pjsua.c +++ b/pjsip/src/pjsua/pjsua.c @@ -23,8 +23,7 @@ struct pjsua pjsua; #define THIS_FILE "pjsua.c" -#define PJSUA_LOCAL_URI "" -#define PJSUA_CONTACT_URI "" +#define PJSUA_LOCAL_URI "" static char *PJSUA_DUMMY_SDP_OFFER = "v=0\r\n" @@ -71,6 +70,10 @@ void pjsua_default(void) /* Default: do not use STUN: */ pjsua.stun_port1 = pjsua.stun_port2 = 0; + + /* Default URIs: */ + + pjsua.local_uri = pj_str(PJSUA_LOCAL_URI); } @@ -129,7 +132,7 @@ static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata) */ static void pjsua_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e) { - ui_inv_on_state_changed(inv, e); + pjsua_ui_inv_on_state_changed(inv, e); } @@ -396,46 +399,7 @@ static pj_status_t init_stack(void) } - /* Add UDP transport: */ - - { - /* Init the published name for the transport. - * Depending whether STUN is used, this may be the STUN mapped - * address, or socket's bound address. - */ - pjsip_host_port addr_name; - - addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr); - addr_name.host.slen = pj_native_strlen(addr_name.host.ptr); - addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port); - - /* Create UDP transport from previously created UDP socket: */ - - status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock, - &addr_name, 1, NULL); - if (status != PJ_SUCCESS) { - pjsua_perror("Unable to start UDP transport", status); - goto on_error; - } - } - - /* Initialize local user info and contact: */ - - { - pj_strdup2(pjsua.pool, &pjsua.local_uri, PJSUA_LOCAL_URI); - pj_strdup2(pjsua.pool, &pjsua.contact_uri, PJSUA_CONTACT_URI); - } - - /* Initialize global route_set: */ - - PJ_TODO(INIT_GLOBAL_ROUTE_SET); - - - /* Start registration: */ - - PJ_TODO(START_REGISTRATION); - - /* Done? */ + /* Done */ return PJ_SUCCESS; @@ -461,11 +425,11 @@ static int PJ_THREAD_FUNC pjsua_worker_thread(void *arg) /* * Initialize pjsua application. - * This will start the registration process, if registration is configured. + * This will initialize all libraries, create endpoint instance, and register + * pjsip modules. */ pj_status_t pjsua_init(void) { - int i; /* Must be signed */ pj_status_t status; /* Init PJLIB logging: */ @@ -491,25 +455,142 @@ pj_status_t pjsua_init(void) pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL); - /* Init sockets (STUN etc): */ + /* Init PJSIP and all the modules: */ - status = init_sockets(); + status = init_stack(); if (status != PJ_SUCCESS) { pj_caching_pool_destroy(&pjsua.cp); - pjsua_perror("init_sockets() has returned error", status); + pjsua_perror("Stack initialization has returned error", status); return status; } + /* Done. */ + return PJ_SUCCESS; +} + - /* Init PJSIP and all the modules: */ - status = init_stack(); +/* + * Start pjsua stack. + * This will start the registration process, if registration is configured. + */ +pj_status_t pjsua_start(void) +{ + int i; /* Must be signed */ + pjsip_transport *udp_transport; + pj_status_t status; + + /* Init sockets (STUN etc): */ + + status = init_sockets(); if (status != PJ_SUCCESS) { pj_caching_pool_destroy(&pjsua.cp); - pjsua_perror("Stack initialization has returned error", status); + pjsua_perror("init_sockets() has returned error", status); return status; } + + /* Add UDP transport: */ + + { + /* Init the published name for the transport. + * Depending whether STUN is used, this may be the STUN mapped + * address, or socket's bound address. + */ + pjsip_host_port addr_name; + + addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr); + addr_name.host.slen = pj_native_strlen(addr_name.host.ptr); + addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port); + + /* Create UDP transport from previously created UDP socket: */ + + status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock, + &addr_name, 1, + &udp_transport); + if (status != PJ_SUCCESS) { + pjsua_perror("Unable to start UDP transport", status); + return status; + } + } + + /* Initialize Contact URI, if one is not specified: */ + + if (pjsua.contact_uri.slen == 0 && pjsua.local_uri.slen) { + + pjsip_uri *uri; + pjsip_sip_uri *sip_uri; + char contact[128]; + int len; + + /* The local Contact is the username@ip-addr, where + * - username is taken from the local URI, + * - ip-addr in UDP transport's address name (which may have been + * resolved from STUN. + */ + + /* Need to parse local_uri to get the elements: */ + + uri = pjsip_parse_uri(pjsua.pool, pjsua.local_uri.ptr, + pjsua.local_uri.slen, 0); + if (uri == NULL) { + pjsua_perror("Invalid local URI", PJSIP_EINVALIDURI); + return PJSIP_EINVALIDURI; + } + + + /* Local URI MUST be a SIP or SIPS: */ + + if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) { + pjsua_perror("Invalid local URI", PJSIP_EINVALIDSCHEME); + return PJSIP_EINVALIDSCHEME; + } + + + /* Get the SIP URI object: */ + + sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); + + + /* Build temporary contact string. */ + + if (sip_uri->user.slen) { + + /* With the user part. */ + len = pj_snprintf(contact, sizeof(contact), + "", + sip_uri->user.slen, + sip_uri->user.ptr, + udp_transport->local_name.host.slen, + udp_transport->local_name.host.ptr, + udp_transport->local_name.port); + } else { + + /* Without user part */ + + len = pj_snprintf(contact, sizeof(contact), + "", + udp_transport->local_name.host.slen, + udp_transport->local_name.host.ptr, + udp_transport->local_name.port); + } + + if (len < 1 || len >= sizeof(contact)) { + pjsua_perror("Invalid Contact", PJSIP_EURITOOLONG); + return PJSIP_EURITOOLONG; + } + + /* Duplicate Contact uri. */ + + pj_strdup2(pjsua.pool, &pjsua.contact_uri, contact); + + } + + /* Initialize global route_set: */ + + PJ_TODO(INIT_GLOBAL_ROUTE_SET); + + /* Create worker thread(s), if required: */ for (i=0; iauth_sess, pjsua.cred_count, + pjsua.cred_info); /* Create initial INVITE: */ -- cgit v1.2.3