sipstack.c 4.98 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "belle_sip_internal.h"
jehan's avatar
jehan committed
20
#include "listeningpoint_internal.h"
21 22 23
#ifdef HAVE_GNUTLS
#include <gnutls/gnutls.h>
#endif
24
static void belle_sip_stack_destroy(belle_sip_stack_t *stack){
jehan's avatar
jehan committed
25 26 27
#ifdef HAVE_GNUTLS
	gnutls_global_deinit ();
#endif
jehan's avatar
jehan committed
28
	belle_sip_object_unref(stack->ml);
29 30
}

31
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_stack_t);
Simon Morlat's avatar
Simon Morlat committed
32
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_stack_t,belle_sip_object_t,belle_sip_stack_destroy,NULL,NULL,FALSE);
33 34 35 36 37 38 39 40 41 42 43 44
#ifdef HAVE_GNUTLS
static void _gnutls_log_func( int level, const char* log) {
	belle_sip_log_level belle_sip_level;
	switch(level) {
	case 1: belle_sip_level=BELLE_SIP_LOG_ERROR;break;
	case 2: belle_sip_level=BELLE_SIP_LOG_WARNING;break;
	case 3: belle_sip_level=BELLE_SIP_LOG_MESSAGE;break;
	default:belle_sip_level=BELLE_SIP_LOG_MESSAGE;break;
	}
	belle_sip_log(belle_sip_level,"gnutls:%s",log);
}
#endif /*HAVE_GNUTLS*/
45
belle_sip_stack_t * belle_sip_stack_new(const char *properties){
jehan's avatar
jehan committed
46
#ifdef HAVE_GNUTLS
47
	int result;
jehan's avatar
jehan committed
48
#endif
Simon Morlat's avatar
Simon Morlat committed
49
	belle_sip_stack_t *stack=belle_sip_object_new(belle_sip_stack_t);
50
	stack->ml=belle_sip_main_loop_new ();
51 52 53
	stack->timer_config.T1=500;
	stack->timer_config.T2=4000;
	stack->timer_config.T4=5000;
54
	stack->transport_timeout=30000;
55
#ifdef HAVE_OPENSSL
jehan's avatar
jehan committed
56 57 58 59
	SSL_library_init();
	SSL_load_error_strings();
	/*CRYPTO_set_id_callback(&threadid_cb);
	CRYPTO_set_locking_callback(&locking_function);*/
60 61
#endif
#ifdef HAVE_GNUTLS
62 63 64 65
	/*gnutls_global_set_log_level(9);*/
	gnutls_global_set_log_function(_gnutls_log_func);
	if ((result = gnutls_global_init ()) <0) {
		belle_sip_fatal("Cannot initialize gnu tls caused by [%s]",gnutls_strerror(result));
66
	}
jehan's avatar
jehan committed
67
#endif
68 69 70
	return stack;
}

71 72 73 74
const belle_sip_timer_config_t *belle_sip_stack_get_timer_config(const belle_sip_stack_t *stack){
	return &stack->timer_config;
}

75 76 77 78
int belle_sip_stack_get_transport_timeout(const belle_sip_stack_t *stack){
	return stack->transport_timeout;
}

79 80
belle_sip_listening_point_t *belle_sip_stack_create_listening_point(belle_sip_stack_t *s, const char *ipaddress, int port, const char *transport){
	belle_sip_listening_point_t *lp=NULL;
jehan's avatar
jehan committed
81
	if (strcasecmp(transport,"UDP")==0) {
82
		lp=belle_sip_udp_listening_point_new(s,ipaddress,port);
jehan's avatar
jehan committed
83 84
	} else if (strcasecmp(transport,"TCP") == 0) {
		lp=belle_sip_stream_listening_point_new(s,ipaddress,port);
85 86
	}else if (strcasecmp(transport,"TLS") == 0) {
		lp=belle_sip_tls_listening_point_new(s,ipaddress,port);
jehan's avatar
jehan committed
87
	} else {
88 89 90 91 92
		belle_sip_fatal("Unsupported transport %s",transport);
	}
	return lp;
}

93 94 95 96
void belle_sip_stack_delete_listening_point(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
	belle_sip_object_unref(lp);
}

97
belle_sip_provider_t *belle_sip_stack_create_provider(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
98 99 100 101 102 103
	belle_sip_provider_t *p=belle_sip_provider_new(s,lp);
	return p;
}

void belle_sip_stack_delete_provider(belle_sip_stack_t *s, belle_sip_provider_t *p){
	belle_sip_object_unref(p);
104 105
}

106 107 108 109
belle_sip_main_loop_t * belle_sip_stack_get_main_loop(belle_sip_stack_t *stack){
	return stack->ml;
}

110 111 112 113 114 115 116
void belle_sip_stack_main(belle_sip_stack_t *stack){
	belle_sip_main_loop_run(stack->ml);
}

void belle_sip_stack_sleep(belle_sip_stack_t *stack, unsigned int milliseconds){
	belle_sip_main_loop_sleep (stack->ml,milliseconds);
}
117 118

void belle_sip_stack_get_next_hop(belle_sip_stack_t *stack, belle_sip_request_t *req, belle_sip_hop_t *hop){
119 120
	belle_sip_header_route_t *route=BELLE_SIP_HEADER_ROUTE(belle_sip_message_get_header(BELLE_SIP_MESSAGE(req),"route"));
	belle_sip_uri_t *uri;
121
	const char *transport;
122 123 124 125 126
	if (route!=NULL){
		uri=belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(route));
	}else{
		uri=belle_sip_request_get_uri(req);
	}
127 128 129 130
	transport=belle_sip_uri_get_transport_param(uri);
	if (transport!=NULL) hop->transport=belle_sip_strdup(transport);
	else hop->transport=NULL;
	hop->host=belle_sip_strdup(belle_sip_uri_get_host(uri));
131
	hop->port=belle_sip_uri_get_listening_port(uri);
132 133 134
	if (route){
		belle_sip_message_remove_first((belle_sip_message_t*)req,"route");
	}
135
}
136

137 138 139 140 141 142 143 144 145 146
void belle_sip_hop_free(belle_sip_hop_t *hop){
	if (hop->host) {
		belle_sip_free(hop->host);
		hop->host=NULL;
	}
	if (hop->transport){
		belle_sip_free(hop->transport);
		hop->transport=NULL;
	}
}
147

Simon Morlat's avatar
Simon Morlat committed
148 149 150 151
void belle_sip_stack_set_tx_delay(belle_sip_stack_t *stack, int delay_ms){
	stack->tx_delay=delay_ms;
}

jehan's avatar
jehan committed
152 153 154
const char* belle_sip_version_to_string() {
	return PACKAGE_VERSION;
}