sipstack.c 8.81 KB
Newer Older
1 2 3 4 5 6
/*
	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
7
    the Free Software Foundation, either version 2 of the License, or
8 9 10 11 12 13 14 15 16 17 18 19
    (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"
Simon Morlat's avatar
Simon Morlat committed
21 22


23
belle_sip_hop_t* belle_sip_hop_new(const char* transport, const char *cname, const char* host,int port) {
Simon Morlat's avatar
Simon Morlat committed
24 25 26
	belle_sip_hop_t* hop = belle_sip_object_new(belle_sip_hop_t);
	if (transport) hop->transport=belle_sip_strdup(transport);
	if (host) hop->host=belle_sip_strdup(host);
27
	if (cname) hop->cname=belle_sip_strdup(cname);
Simon Morlat's avatar
Simon Morlat committed
28 29 30 31 32 33
	hop->port=port;
	return hop;
}

belle_sip_hop_t* belle_sip_hop_new_from_uri(const belle_sip_uri_t *uri){
	const char *host;
34
	const char *cname=NULL;
jehan's avatar
jehan committed
35 36 37 38
	const char * transport=belle_sip_uri_get_transport_param(uri);
	if (!transport) {
		transport=belle_sip_uri_is_secure(uri)?"tls":"udp";
	}
Simon Morlat's avatar
Simon Morlat committed
39 40
	host=belle_sip_uri_get_maddr_param(uri);
	if (!host) host=belle_sip_uri_get_host(uri);
41
	else cname=belle_sip_uri_get_host(uri);
jehan's avatar
jehan committed
42 43 44 45 46

	return belle_sip_hop_new(	transport,
								cname,
								host,
								belle_sip_uri_get_listening_port(uri));
Simon Morlat's avatar
Simon Morlat committed
47 48
}

Simon Morlat's avatar
Simon Morlat committed
49 50 51 52
belle_sip_hop_t* belle_sip_hop_new_from_generic_uri(const belle_generic_uri_t *uri){
	const char *host;
	const char * transport="TCP";
	const char *scheme=belle_generic_uri_get_scheme(uri);
Simon Morlat's avatar
Simon Morlat committed
53 54
	int port=belle_generic_uri_get_port(uri);
	int well_known_port=0;
Simon Morlat's avatar
Simon Morlat committed
55 56
	
	host=belle_generic_uri_get_host(uri);
Simon Morlat's avatar
Simon Morlat committed
57 58 59 60 61 62 63
	if (strcasecmp(scheme,"http")==0) {
		transport="TCP";
		well_known_port=80;
	}else if (strcasecmp(scheme,"https")==0) {
		transport="TLS";
		well_known_port=443;
	}
Simon Morlat's avatar
Simon Morlat committed
64 65 66 67

	return belle_sip_hop_new(transport,
				host,
				host,
Simon Morlat's avatar
Simon Morlat committed
68
				port > 0 ? port : well_known_port);
Simon Morlat's avatar
Simon Morlat committed
69 70
}

Simon Morlat's avatar
Simon Morlat committed
71 72 73 74 75
static void belle_sip_hop_destroy(belle_sip_hop_t *hop){
	if (hop->host) {
		belle_sip_free(hop->host);
		hop->host=NULL;
	}
76 77 78 79
	if (hop->cname){
		belle_sip_free(hop->cname);
		hop->cname=NULL;
	}
Simon Morlat's avatar
Simon Morlat committed
80 81 82 83 84 85
	if (hop->transport){
		belle_sip_free(hop->transport);
		hop->transport=NULL;
	}
}

86 87 88
static void belle_sip_hop_clone(belle_sip_hop_t *hop, const belle_sip_hop_t *orig){
	if (orig->host)
		hop->host=belle_sip_strdup(orig->host);
89 90
	if (orig->cname)
		hop->cname=belle_sip_strdup(orig->cname);
91 92 93 94 95
	if (orig->transport)
		hop->transport=belle_sip_strdup(orig->transport);
	
}

Simon Morlat's avatar
Simon Morlat committed
96
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_hop_t);
97
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_hop_t,belle_sip_object_t,belle_sip_hop_destroy,belle_sip_hop_clone,NULL,TRUE);
Simon Morlat's avatar
Simon Morlat committed
98

99
static void belle_sip_stack_destroy(belle_sip_stack_t *stack){
Simon Morlat's avatar
Simon Morlat committed
100
	belle_sip_message("stack [%p] destroyed.",stack);
jehan's avatar
jehan committed
101
	if (stack->dns_user_hosts_file) belle_sip_free(stack->dns_user_hosts_file);
Simon Morlat's avatar
Simon Morlat committed
102
	if (stack->dns_resolv_conf) belle_sip_free(stack->dns_resolv_conf);
jehan's avatar
jehan committed
103
	belle_sip_object_unref(stack->ml);
104 105
}

106
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_stack_t);
Simon Morlat's avatar
Simon Morlat committed
107
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_stack_t,belle_sip_object_t,belle_sip_stack_destroy,NULL,NULL,FALSE);
Simon Morlat's avatar
Simon Morlat committed
108

109
belle_sip_stack_t * belle_sip_stack_new(const char *properties){
Simon Morlat's avatar
Simon Morlat committed
110
	belle_sip_stack_t *stack=belle_sip_object_new(belle_sip_stack_t);
111
	stack->ml=belle_sip_main_loop_new ();
112 113 114
	stack->timer_config.T1=500;
	stack->timer_config.T2=4000;
	stack->timer_config.T4=5000;
115
	stack->transport_timeout=63000;
Simon Morlat's avatar
Simon Morlat committed
116
	stack->dns_timeout=15000;
117
	stack->dns_srv_enabled=TRUE;
118
	stack->inactive_transport_timeout=3600; /*one hour*/
119 120 121
	return stack;
}

122 123 124 125
const belle_sip_timer_config_t *belle_sip_stack_get_timer_config(const belle_sip_stack_t *stack){
	return &stack->timer_config;
}

jehan's avatar
jehan committed
126 127 128 129 130 131 132 133 134
void belle_sip_stack_set_timer_config(belle_sip_stack_t *stack,const belle_sip_timer_config_t *timer_config){
	belle_sip_message("Setting timer config to T1 [%i], T2 [%i], T3 [%i], T4 [%i] on stack [%p]", timer_config->T1
																								, timer_config->T2
																								, timer_config->T3
																								, timer_config->T4
																								, stack);
	stack->timer_config=*timer_config;
}

135 136 137 138
void belle_sip_stack_set_transport_timeout(belle_sip_stack_t *stack, int timeout_ms){
	stack->transport_timeout=timeout_ms;
}

139 140 141 142
int belle_sip_stack_get_transport_timeout(const belle_sip_stack_t *stack){
	return stack->transport_timeout;
}

143 144 145 146
int belle_sip_stack_get_dns_timeout(const belle_sip_stack_t *stack) {
	return stack->dns_timeout;
}

147 148 149 150
void belle_sip_stack_set_dns_timeout(belle_sip_stack_t *stack, int timeout) {
	stack->dns_timeout = timeout;
}

151 152 153 154 155 156 157 158
unsigned char belle_sip_stack_dns_srv_enabled(const belle_sip_stack_t *stack) {
	return stack->dns_srv_enabled;
}

void belle_sip_stack_enable_dns_srv(belle_sip_stack_t *stack, unsigned char enable) {
	stack->dns_srv_enabled = enable;
}

159 160
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
161
	if (strcasecmp(transport,"UDP")==0) {
162
		lp=belle_sip_udp_listening_point_new(s,ipaddress,port);
jehan's avatar
jehan committed
163 164
	} else if (strcasecmp(transport,"TCP") == 0) {
		lp=belle_sip_stream_listening_point_new(s,ipaddress,port);
165 166
	}else if (strcasecmp(transport,"TLS") == 0) {
		lp=belle_sip_tls_listening_point_new(s,ipaddress,port);
jehan's avatar
jehan committed
167
	} else {
168 169 170 171 172
		belle_sip_fatal("Unsupported transport %s",transport);
	}
	return lp;
}

173 174 175 176
void belle_sip_stack_delete_listening_point(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
	belle_sip_object_unref(lp);
}

177
belle_sip_provider_t *belle_sip_stack_create_provider(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
178 179 180 181
	belle_sip_provider_t *p=belle_sip_provider_new(s,lp);
	return p;
}

Simon Morlat's avatar
Simon Morlat committed
182 183
belle_http_provider_t *belle_sip_stack_create_http_provider(belle_sip_stack_t *s, const char *bind_ip){
	belle_http_provider_t *p=belle_http_provider_new(s, bind_ip);
Simon Morlat's avatar
Simon Morlat committed
184 185 186
	return p;
}

187 188
void belle_sip_stack_delete_provider(belle_sip_stack_t *s, belle_sip_provider_t *p){
	belle_sip_object_unref(p);
189 190
}

191 192 193 194
belle_sip_main_loop_t * belle_sip_stack_get_main_loop(belle_sip_stack_t *stack){
	return stack->ml;
}

195 196 197 198 199 200 201
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);
}
202

Simon Morlat's avatar
Simon Morlat committed
203
belle_sip_hop_t * belle_sip_stack_get_next_hop(belle_sip_stack_t *stack, belle_sip_request_t *req) {
204 205
	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;
Simon Morlat's avatar
Simon Morlat committed
206

207 208 209 210 211
	if (route!=NULL){
		uri=belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(route));
	}else{
		uri=belle_sip_request_get_uri(req);
	}
Simon Morlat's avatar
Simon Morlat committed
212
	return belle_sip_hop_new_from_uri(uri);
213
}
214

Simon Morlat's avatar
Simon Morlat committed
215 216


Simon Morlat's avatar
Simon Morlat committed
217 218 219
void belle_sip_stack_set_tx_delay(belle_sip_stack_t *stack, int delay_ms){
	stack->tx_delay=delay_ms;
}
220 221 222
void belle_sip_stack_set_send_error(belle_sip_stack_t *stack, int send_error){
	stack->send_error=send_error;
}
Simon Morlat's avatar
Simon Morlat committed
223

224 225 226 227 228 229 230 231
void belle_sip_stack_set_resolver_tx_delay(belle_sip_stack_t *stack, int delay_ms) {
	stack->resolver_tx_delay = delay_ms;
}

void belle_sip_stack_set_resolver_send_error(belle_sip_stack_t *stack, int send_error) {
	stack->resolver_send_error = send_error;
}

232 233 234 235 236
const char * belle_sip_stack_get_dns_user_hosts_file(const belle_sip_stack_t *stack) {
	return stack->dns_user_hosts_file;
}

void belle_sip_stack_set_dns_user_hosts_file(belle_sip_stack_t *stack, const char *hosts_file) {
jehan's avatar
jehan committed
237 238
	if (stack->dns_user_hosts_file) belle_sip_free(stack->dns_user_hosts_file);
	stack->dns_user_hosts_file = hosts_file?belle_sip_strdup(hosts_file):NULL;
239 240
}

Simon Morlat's avatar
Simon Morlat committed
241 242 243 244 245 246 247 248 249
const char * belle_sip_stack_get_dns_resolv_conf_file(const belle_sip_stack_t *stack){
	return stack->dns_resolv_conf;
}

void belle_sip_stack_set_dns_resolv_conf_file(belle_sip_stack_t *stack, const char *resolv_conf_file){
	if (stack->dns_resolv_conf) belle_sip_free(stack->dns_resolv_conf);
	stack->dns_resolv_conf = resolv_conf_file?belle_sip_strdup(resolv_conf_file):NULL;
}

jehan's avatar
jehan committed
250
const char* belle_sip_version_to_string() {
251 252 253
#ifdef BELLESIP_VERSION
	return BELLESIP_VERSION;
#else
jehan's avatar
jehan committed
254
	return PACKAGE_VERSION;
255
#endif
jehan's avatar
jehan committed
256
}
257

258 259 260 261 262 263 264
int belle_sip_stack_get_inactive_transport_timeout(const belle_sip_stack_t *stack){
	return stack->inactive_transport_timeout;
}

void belle_sip_stack_set_inactive_transport_timeout(belle_sip_stack_t *stack, int seconds){
	stack->inactive_transport_timeout=seconds;
}
Simon Morlat's avatar
Simon Morlat committed
265 266 267 268 269 270 271 272 273

void belle_sip_stack_set_default_dscp(belle_sip_stack_t *stack, int dscp){
	stack->dscp=dscp;
}

int belle_sip_stack_get_default_dscp(belle_sip_stack_t *stack){
	return stack->dscp;
}

274 275 276 277
int belle_sip_stack_tls_available(belle_sip_stack_t *stack){
	return belle_sip_tls_listening_point_available();
}