listeningpoint.c 8.37 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
    (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/>.
*/

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
	char *tmp;
24
	belle_sip_init_sockets();
25
	lp->stack=s;
jehan's avatar
jehan committed
26
	lp->listening_uri=belle_sip_uri_create(NULL,address);
27
	belle_sip_object_ref(lp->listening_uri);
jehan's avatar
jehan committed
28 29
	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);
Simon Morlat's avatar
Simon Morlat committed
30
	tmp=belle_sip_object_to_string((belle_sip_object_t*)BELLE_SIP_LISTENING_POINT(lp)->listening_uri);
31 32 33 34 35
	if (strchr(address,':')) {
		lp->ai_family=AF_INET6;
	} else {
		lp->ai_family=AF_INET;
	}
Simon Morlat's avatar
Simon Morlat committed
36 37
	belle_sip_message("Creating listening point [%p] on [%s]",lp, tmp);
	belle_sip_free(tmp);
38 39 40
}

static void belle_sip_listening_point_uninit(belle_sip_listening_point_t *lp){
41
	char *tmp=belle_sip_object_to_string((belle_sip_object_t*)BELLE_SIP_LISTENING_POINT(lp)->listening_uri);
42
	belle_sip_listening_point_clean_channels(lp);
43
	belle_sip_message("Listening point [%p] on [%s] destroyed",lp, tmp);
jehan's avatar
jehan committed
44
	belle_sip_object_unref(lp->listening_uri);
45
	belle_sip_free(tmp);
46
	lp->channel_listener=NULL; /*does not unref provider*/
47
	belle_sip_uninit_sockets();
jehan's avatar
jehan committed
48
	belle_sip_listening_point_set_keep_alive(lp,-1);
49 50
}

51

jehan's avatar
jehan committed
52
void belle_sip_listening_point_add_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
53
	chan->lp=lp;
54
	belle_sip_channel_add_listener(chan,lp->channel_listener); /*add channel listener*/
55 56 57 58 59 60 61 62 63 64 65
	/*channel is already owned, no ref needed - REVISIT: channel should be initally unowned probably.*/
	
	/* The channel with names must be treated with higher priority by the get_channel() method so queued on front.
	 * This is to prevent the UDP listening point to dispatch incoming messages to channels that were created by inbound connection
	 * where name cannot be determined. When this arrives, there can be 2 channels for the same destination IP and strange problems can occur
	 * where requests are sent through name qualified channel and response received through name unqualified channel.
	 */
	if (chan->has_name)
		lp->channels=belle_sip_list_prepend(lp->channels,chan);
	else
		lp->channels=belle_sip_list_append(lp->channels,chan);
66 67
}

68 69
belle_sip_channel_t *belle_sip_listening_point_create_channel(belle_sip_listening_point_t *obj, const belle_sip_hop_t *hop){
	belle_sip_channel_t *chan=BELLE_SIP_OBJECT_VPTR(obj,belle_sip_listening_point_t)->create_channel(obj,hop);
70 71 72 73
	if (chan){
		belle_sip_listening_point_add_channel(obj,chan);
	}
	return chan;
74 75
}

Simon Morlat's avatar
Simon Morlat committed
76 77

void belle_sip_listening_point_remove_channel(belle_sip_listening_point_t *lp, belle_sip_channel_t *chan){
78
	belle_sip_channel_remove_listener(chan,lp->channel_listener);
79
	lp->channels=belle_sip_list_remove(lp->channels,chan);
80
	belle_sip_object_unref(chan);
81
}
Simon Morlat's avatar
Simon Morlat committed
82

83

84
void belle_sip_listening_point_clean_channels(belle_sip_listening_point_t *lp){
85
	int existing_channels = belle_sip_listening_point_get_channel_count(lp);
86
	belle_sip_list_t* iterator;
Simon Morlat's avatar
Simon Morlat committed
87
	
88
	if (existing_channels > 0) {
89
		belle_sip_message("Listening point destroying [%i] channels",existing_channels);
90
	}
Simon Morlat's avatar
Simon Morlat committed
91 92 93
	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);
94 95
	}
	lp->channels=belle_sip_list_free_with_data(lp->channels,(void (*)(void*))belle_sip_object_unref);
96 97
}

98
int belle_sip_listening_point_get_channel_count(const belle_sip_listening_point_t *lp){
99
	return (int)belle_sip_list_size(lp->channels);
100
}
101

102
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_listening_point_t);
103
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_listening_point_t)
104
	{ 
105
		BELLE_SIP_VPTR_INIT(belle_sip_listening_point_t, belle_sip_object_t,FALSE),
106 107
		(belle_sip_object_destroy_t)belle_sip_listening_point_uninit,
		NULL,
108
		NULL,
109 110
		(belle_sip_object_on_first_ref_t) NULL,
		(belle_sip_object_on_last_ref_t) NULL,
111
		BELLE_SIP_DEFAULT_BUFSIZE_HINT
112 113 114
	},
	NULL,
	NULL
115
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
116

117
const char *belle_sip_listening_point_get_ip_address(const belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
118
	return belle_sip_uri_get_host(lp->listening_uri);
119 120 121
}

int belle_sip_listening_point_get_port(const belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
122
	return belle_sip_uri_get_listening_port(lp->listening_uri);
123 124 125
}

const char *belle_sip_listening_point_get_transport(const belle_sip_listening_point_t *lp){
jehan's avatar
jehan committed
126
	return belle_sip_uri_get_transport_param(lp->listening_uri);
127 128
}

jehan's avatar
jehan committed
129 130 131
const belle_sip_uri_t* belle_sip_listening_point_get_uri(const  belle_sip_listening_point_t *lp) {
	return lp->listening_uri;
}
132 133 134 135 136 137 138
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;
}

139
belle_sip_channel_t *_belle_sip_listening_point_get_channel(belle_sip_listening_point_t *lp, const belle_sip_hop_t *hop, const struct addrinfo *addr){
Simon Morlat's avatar
Simon Morlat committed
140
	return belle_sip_channel_find_from_list_with_addrinfo(lp->channels,hop,addr);
141 142
}

143
belle_sip_channel_t *belle_sip_listening_point_get_channel(belle_sip_listening_point_t *lp,const belle_sip_hop_t *hop){
Simon Morlat's avatar
Simon Morlat committed
144
	return belle_sip_channel_find_from_list(lp->channels,lp->ai_family,hop);
145 146
}

jehan's avatar
jehan committed
147 148
static int send_keep_alive(belle_sip_channel_t* obj) {
	/*keep alive*/
Simon Morlat's avatar
Simon Morlat committed
149
	const char* crlfcrlf = "\r\n\r\n";
150
	size_t size=strlen(crlfcrlf);
151 152 153
	int err=belle_sip_channel_send(obj,crlfcrlf,size);
	
	if (err<=0 && !belle_sip_error_code_is_would_block(-err) && err!=-EINTR){
154 155
		belle_sip_error("channel [%p]: could not send [%u] bytes of keep alive from [%s://%s:%i]  to [%s:%i]"	,obj
			,(unsigned int)size
Simon Morlat's avatar
Simon Morlat committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
			,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
171 172 173 174 175
}
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;
176 177 178
	belle_sip_list_t *to_be_closed=NULL;

	for (iterator=lp->channels;iterator!=NULL;iterator=iterator->next) {
179
		channel=(belle_sip_channel_t*)iterator->data;
180 181
		if (channel->state == BELLE_SIP_CHANNEL_READY && send_keep_alive(channel)==-1) { /*only send keep alive if ready*/
			to_be_closed=belle_sip_list_append(to_be_closed,channel);
182
		}
jehan's avatar
jehan committed
183
	}
184 185 186 187 188 189
	for (iterator=to_be_closed;iterator!=NULL;iterator=iterator->next){
		channel=(belle_sip_channel_t*)iterator->data;
		channel_set_state(channel,BELLE_SIP_CHANNEL_ERROR);
		belle_sip_channel_close(channel);
	}
	belle_sip_list_free(to_be_closed);
190
	return BELLE_SIP_CONTINUE_WITHOUT_CATCHUP;
jehan's avatar
jehan committed
191 192
}

Simon Morlat's avatar
Simon Morlat committed
193
void belle_sip_listening_point_set_keep_alive(belle_sip_listening_point_t *lp,int ms) {
jehan's avatar
jehan committed
194
	if (ms <=0) {
195
		if (lp->keep_alive_timer) {
jehan's avatar
jehan committed
196 197 198 199 200 201 202 203 204
			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
205 206 207 208
			, keep_alive_timer_func
			, lp
			, ms
			,"keep alive") ;
jehan's avatar
jehan committed
209 210 211 212 213 214 215
	} 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) {
216
	return lp->keep_alive_timer?(int)belle_sip_source_get_timeout(lp->keep_alive_timer):-1;
jehan's avatar
jehan committed
217
}
218

219
void belle_sip_listening_point_set_channel_listener(belle_sip_listening_point_t *lp,belle_sip_channel_listener_t* channel_listener) {
220 221
	lp->channel_listener=channel_listener;
}