listeningpoint.c 7.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
	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/>.
*/

Simon Morlat's avatar
Simon Morlat committed
19
#include "belle_sip_internal.h"
20 21


jehan's avatar
jehan committed
22
void belle_sip_listening_point_init(belle_sip_listening_point_t *lp, belle_sip_stack_t *s, const char *address, int port){
Simon Morlat's avatar
Simon Morlat committed
23
	belle_sip_init_sockets();
24
	lp->stack=s;
jehan's avatar
jehan committed
25
	lp->listening_uri=belle_sip_uri_create(NULL,address);
26
	belle_sip_object_ref(lp->listening_uri);
jehan's avatar
jehan committed
27 28
	belle_sip_uri_set_port(lp->listening_uri,port);
	belle_sip_uri_set_transport_param(lp->listening_uri,BELLE_SIP_OBJECT_VPTR(lp,belle_sip_listening_point_t)->transport);
29 30 31
}

static void belle_sip_listening_point_uninit(belle_sip_listening_point_t *lp){
32
	char *tmp=belle_sip_object_to_string((belle_sip_object_t*)BELLE_SIP_LISTENING_POINT(lp)->listening_uri);
33
	belle_sip_listening_point_clean_channels(lp);
34
	belle_sip_message("Listening point [%p] on [%s] destroyed",lp, tmp);
jehan's avatar
jehan committed
35
	belle_sip_object_unref(lp->listening_uri);
36
	belle_sip_free(tmp);
37
	lp->channel_listener=NULL; /*does not unref provider*/
Simon Morlat's avatar
Simon Morlat committed
38
	belle_sip_uninit_sockets();
jehan's avatar
jehan committed
39
	belle_sip_listening_point_set_keep_alive(lp,-1);
40 41
}

42

jehan's avatar
jehan committed
43
void belle_sip_listening_point_add_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
44
	belle_sip_channel_add_listener(chan,lp->channel_listener); /*add channel listener*/
jehan's avatar
jehan committed
45
	lp->channels=belle_sip_list_append(lp->channels,chan);/*channel is already owned*/
46 47 48 49 50
}

belle_sip_channel_t *belle_sip_listening_point_create_channel(belle_sip_listening_point_t *obj, const char *dest, int port){
	belle_sip_channel_t *chan=BELLE_SIP_OBJECT_VPTR(obj,belle_sip_listening_point_t)->create_channel(obj,dest,port);
	if (chan){
Simon Morlat's avatar
wip  
Simon Morlat committed
51
		chan->lp=obj;
52 53 54
		belle_sip_listening_point_add_channel(obj,chan);
	}
	return chan;
55 56
}

Simon Morlat's avatar
wip  
Simon Morlat committed
57 58

void belle_sip_listening_point_remove_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
59
	belle_sip_channel_remove_listener(chan,lp->channel_listener);
60
	lp->channels=belle_sip_list_remove(lp->channels,chan);
61
	belle_sip_object_unref(chan);
62
}
Simon Morlat's avatar
wip  
Simon Morlat committed
63

64

65 66
void belle_sip_listening_point_clean_channels(belle_sip_listening_point_t *lp){
	int existing_channels;
67
	belle_sip_list_t* iterator;
Simon Morlat's avatar
Simon Morlat committed
68
	
69
	if ((existing_channels=belle_sip_list_size(lp->channels)) > 0) {
70
		belle_sip_message("Listening point destroying [%i] channels",existing_channels);
71
	}
Simon Morlat's avatar
Simon Morlat committed
72 73 74
	for (iterator=lp->channels;iterator!=NULL;iterator=iterator->next) {
		belle_sip_channel_t *chan=(belle_sip_channel_t*)iterator->data;
		belle_sip_channel_force_close(chan);
75 76
	}
	lp->channels=belle_sip_list_free_with_data(lp->channels,(void (*)(void*))belle_sip_object_unref);
77 78
}

79

80
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_listening_point_t);
81 82
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_listening_point_t)={
	{ 
Simon Morlat's avatar
Simon Morlat committed
83
		BELLE_SIP_VPTR_INIT(belle_sip_listening_point_t, belle_sip_object_t,FALSE),
84 85 86 87 88 89 90
		(belle_sip_object_destroy_t)belle_sip_listening_point_uninit,
		NULL,
		NULL
	},
	NULL,
	NULL
};
Simon Morlat's avatar
Simon Morlat committed
91

92
const char *belle_sip_listening_point_get_ip_address(const belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
93
	return belle_sip_uri_get_host(lp->listening_uri);
94 95 96
}

int belle_sip_listening_point_get_port(const belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
97
	return belle_sip_uri_get_listening_port(lp->listening_uri);
98 99 100
}

const char *belle_sip_listening_point_get_transport(const belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
101
	return belle_sip_uri_get_transport_param(lp->listening_uri);
102 103
}

jehan's avatar
jehan committed
104 105 106
const belle_sip_uri_t* belle_sip_listening_point_get_uri(const  belle_sip_listening_point_t *lp) {
	return lp->listening_uri;
}
107 108 109 110 111 112 113
int belle_sip_listening_point_get_well_known_port(const char *transport){
	if (strcasecmp(transport,"UDP")==0 || strcasecmp(transport,"TCP")==0 ) return 5060;
	if (strcasecmp(transport,"DTLS")==0 || strcasecmp(transport,"TLS")==0 ) return 5061;
	belle_sip_error("No well known port for transport %s", transport);
	return -1;
}

jehan's avatar
jehan committed
114
belle_sip_channel_t *_belle_sip_listening_point_get_channel(belle_sip_listening_point_t *lp,const char *peer_name, int peer_port, const struct addrinfo *addr){
115
	belle_sip_list_t *elem;
116
	belle_sip_channel_t *chan;
Simon Morlat's avatar
Simon Morlat committed
117 118 119 120 121 122 123 124 125 126 127
	
	for(elem=lp->channels;elem!=NULL;elem=elem->next){
		chan=(belle_sip_channel_t*)elem->data;
		if (belle_sip_channel_matches(chan,peer_name,peer_port,addr)){
			return chan;
		}
	}
	return NULL;
}

belle_sip_channel_t *belle_sip_listening_point_get_channel(belle_sip_listening_point_t *lp,const char *peer_name, int peer_port){
128 129 130
	struct addrinfo *res=NULL;
	struct addrinfo hints={0};
	char portstr[20];
Simon Morlat's avatar
Simon Morlat committed
131
	belle_sip_channel_t *chan;
132 133 134 135

	hints.ai_flags=AI_NUMERICHOST|AI_NUMERICSERV;
	snprintf(portstr,sizeof(portstr),"%i",peer_port);
	getaddrinfo(peer_name,portstr,&hints,&res);
Simon Morlat's avatar
Simon Morlat committed
136
	chan=_belle_sip_listening_point_get_channel(lp,peer_name,peer_port,res);
137
	if (res) freeaddrinfo(res);
Simon Morlat's avatar
Simon Morlat committed
138
	return chan;
139 140
}

jehan's avatar
jehan committed
141 142
static int send_keep_alive(belle_sip_channel_t* obj) {
	/*keep alive*/
Simon Morlat's avatar
Simon Morlat committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	const char* crlfcrlf = "\r\n\r\n";
	int size=strlen(crlfcrlf);
	if (belle_sip_channel_send(obj,crlfcrlf,size)<0){
		belle_sip_error("channel [%p]: could not send [%i] bytes of keep alive from [%s://%s:%i]  to [%s:%i]"	,obj
			,size
			,belle_sip_channel_get_transport_name(obj)
			,obj->local_ip
			,obj->local_port
			,obj->peer_name
			,obj->peer_port);

		return -1;
	}else{
		belle_sip_message("channel [%p]: keep alive sent to [%s://%s:%i]"
							,obj
							,belle_sip_channel_get_transport_name(obj)
							,obj->peer_name
							,obj->peer_port);
		return 0;
	}
jehan's avatar
jehan committed
163 164 165 166 167
}
static int keep_alive_timer_func(void *user_data, unsigned int events) {
	belle_sip_listening_point_t* lp=(belle_sip_listening_point_t*)user_data;
	belle_sip_list_t* iterator;
	belle_sip_channel_t* channel;
168 169 170 171 172 173 174 175 176
		/*list is copied to make sure it is not altered in case of error*/
	for (iterator=belle_sip_list_copy(lp->channels);iterator!=NULL;iterator=iterator->next) {
		channel=(belle_sip_channel_t*)iterator->data;
		belle_sip_object_ref(channel); /*to make sure channels are still valid even if error is reported*/
		if (channel->state == BELLE_SIP_CHANNEL_READY && send_keep_alive(channel)) { /*only send keep alive if ready*/
			channel_set_state(channel,BELLE_SIP_CHANNEL_ERROR);
			belle_sip_channel_close(channel);
		}
		belle_sip_object_unref(channel);
jehan's avatar
jehan committed
177
	}
178
	belle_sip_list_free(iterator);
jehan's avatar
jehan committed
179 180 181
	return BELLE_SIP_CONTINUE;
}

Simon Morlat's avatar
Simon Morlat committed
182
void belle_sip_listening_point_set_keep_alive(belle_sip_listening_point_t *lp,int ms) {
jehan's avatar
jehan committed
183 184 185 186 187 188 189 190 191 192 193
	if (ms <=0) {
		if(lp->keep_alive_timer) {
			belle_sip_main_loop_remove_source(lp->stack->ml,lp->keep_alive_timer);
			belle_sip_object_unref(lp->keep_alive_timer);
			lp->keep_alive_timer=NULL;
		}
		return;
	}

	if (!lp->keep_alive_timer) {
		lp->keep_alive_timer = belle_sip_main_loop_create_timeout(lp->stack->ml
Simon Morlat's avatar
Simon Morlat committed
194 195 196 197
			, keep_alive_timer_func
			, lp
			, ms
			,"keep alive") ;
jehan's avatar
jehan committed
198 199 200 201 202 203 204 205 206
	} else {
		belle_sip_source_set_timeout(lp->keep_alive_timer,ms);
	}
	return;
}

int belle_sip_listening_point_get_keep_alive(const belle_sip_listening_point_t *lp) {
	return lp->keep_alive_timer?belle_sip_source_get_timeout(lp->keep_alive_timer):-1;
}
207

208 209 210
void belle_sip_listener_set_channel_listener(belle_sip_listening_point_t *lp,belle_sip_channel_listener_t* channel_listener) {
	lp->channel_listener=channel_listener;
}