sipstack.c 5.87 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"
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;
Simon Morlat's avatar
Simon Morlat committed
35 36
	host=belle_sip_uri_get_maddr_param(uri);
	if (!host) host=belle_sip_uri_get_host(uri);
37
	else cname=belle_sip_uri_get_host(uri);
Simon Morlat's avatar
Simon Morlat committed
38
	return belle_sip_hop_new(belle_sip_uri_get_transport_param(uri),
39
				cname,
Simon Morlat's avatar
Simon Morlat committed
40 41 42 43 44 45 46 47 48
				host,
				belle_sip_uri_get_listening_port(uri));
}

static void belle_sip_hop_destroy(belle_sip_hop_t *hop){
	if (hop->host) {
		belle_sip_free(hop->host);
		hop->host=NULL;
	}
49 50 51 52
	if (hop->cname){
		belle_sip_free(hop->cname);
		hop->cname=NULL;
	}
Simon Morlat's avatar
Simon Morlat committed
53 54 55 56 57 58
	if (hop->transport){
		belle_sip_free(hop->transport);
		hop->transport=NULL;
	}
}

59 60 61
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);
62 63
	if (orig->cname)
		hop->cname=belle_sip_strdup(orig->cname);
64 65 66 67 68
	if (orig->transport)
		hop->transport=belle_sip_strdup(orig->transport);
	
}

Simon Morlat's avatar
Simon Morlat committed
69
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_hop_t);
70
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
71

72
static void belle_sip_stack_destroy(belle_sip_stack_t *stack){
jehan's avatar
jehan committed
73
	belle_sip_object_unref(stack->ml);
74 75
}

76
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_stack_t);
Simon Morlat's avatar
Simon Morlat committed
77
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
78

79
belle_sip_stack_t * belle_sip_stack_new(const char *properties){
Simon Morlat's avatar
Simon Morlat committed
80
	belle_sip_stack_t *stack=belle_sip_object_new(belle_sip_stack_t);
81
	stack->ml=belle_sip_main_loop_new ();
82 83 84
	stack->timer_config.T1=500;
	stack->timer_config.T2=4000;
	stack->timer_config.T4=5000;
85
	stack->transport_timeout=30000;
Simon Morlat's avatar
Simon Morlat committed
86
	stack->dns_timeout=15000;
87
	stack->inactive_transport_timeout=3600; /*one hour*/
88 89 90
	return stack;
}

91 92 93 94
const belle_sip_timer_config_t *belle_sip_stack_get_timer_config(const belle_sip_stack_t *stack){
	return &stack->timer_config;
}

95 96 97 98
int belle_sip_stack_get_transport_timeout(const belle_sip_stack_t *stack){
	return stack->transport_timeout;
}

99 100 101 102
int belle_sip_stack_get_dns_timeout(const belle_sip_stack_t *stack) {
	return stack->dns_timeout;
}

103 104 105 106
void belle_sip_stack_set_dns_timeout(belle_sip_stack_t *stack, int timeout) {
	stack->dns_timeout = timeout;
}

107 108
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
109
	if (strcasecmp(transport,"UDP")==0) {
110
		lp=belle_sip_udp_listening_point_new(s,ipaddress,port);
jehan's avatar
jehan committed
111 112
	} else if (strcasecmp(transport,"TCP") == 0) {
		lp=belle_sip_stream_listening_point_new(s,ipaddress,port);
113 114
	}else if (strcasecmp(transport,"TLS") == 0) {
		lp=belle_sip_tls_listening_point_new(s,ipaddress,port);
jehan's avatar
jehan committed
115
	} else {
116 117 118 119 120
		belle_sip_fatal("Unsupported transport %s",transport);
	}
	return lp;
}

121 122 123 124
void belle_sip_stack_delete_listening_point(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
	belle_sip_object_unref(lp);
}

125
belle_sip_provider_t *belle_sip_stack_create_provider(belle_sip_stack_t *s, belle_sip_listening_point_t *lp){
126 127 128 129 130 131
	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);
132 133
}

134 135 136 137
belle_sip_main_loop_t * belle_sip_stack_get_main_loop(belle_sip_stack_t *stack){
	return stack->ml;
}

138 139 140 141 142 143 144
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);
}
145

Simon Morlat's avatar
Simon Morlat committed
146
belle_sip_hop_t * belle_sip_stack_get_next_hop(belle_sip_stack_t *stack, belle_sip_request_t *req) {
147 148
	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
149

150 151 152 153 154
	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
155
	return belle_sip_hop_new_from_uri(uri);
156
}
157

Simon Morlat's avatar
Simon Morlat committed
158 159


Simon Morlat's avatar
Simon Morlat committed
160 161 162
void belle_sip_stack_set_tx_delay(belle_sip_stack_t *stack, int delay_ms){
	stack->tx_delay=delay_ms;
}
163 164 165
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
166

167 168 169 170 171 172 173 174
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;
}

jehan's avatar
jehan committed
175 176 177
const char* belle_sip_version_to_string() {
	return PACKAGE_VERSION;
}
178

179 180 181 182 183 184 185
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;
}