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

Simon Morlat's avatar
Simon Morlat committed
22
const char *belle_sip_channel_state_to_string(belle_sip_channel_state_t state){
23 24 25 26 27 28 29 30 31 32 33 34 35
	switch(state){
		case BELLE_SIP_CHANNEL_INIT:
			return "INIT";
		case BELLE_SIP_CHANNEL_RES_IN_PROGRESS:
			return "RES_IN_PROGRESS";
		case BELLE_SIP_CHANNEL_RES_DONE:
			return "RES_DONE";
		case BELLE_SIP_CHANNEL_CONNECTING:
			return "CONNECTING";
		case BELLE_SIP_CHANNEL_READY:
			return "READY";
		case BELLE_SIP_CHANNEL_ERROR:
			return "ERROR";
jehan's avatar
jehan committed
36
		case BELLE_SIP_CHANNEL_DISCONNECTED:
37
			return "DISCONNECTED";
38 39
	}
	return "BAD";
40
}
41

42 43 44 45 46 47 48 49 50 51
static belle_sip_list_t * for_each_weak_unref_free(belle_sip_list_t *l, belle_sip_object_destroy_notify_t notify, void *ptr){
	belle_sip_list_t *elem,*next;
	for(elem=l;elem!=NULL;elem=next){
		next=elem->next;
		belle_sip_object_weak_unref(elem->data,notify,ptr);
		belle_sip_free(elem);
	}
	return NULL;
}

52
static void belle_sip_channel_destroy(belle_sip_channel_t *obj){
53
	
54 55
	if (obj->peer) freeaddrinfo(obj->peer);
	belle_sip_free(obj->peer_name);
56
	if (obj->local_ip) belle_sip_free(obj->local_ip);
57
	obj->listeners=for_each_weak_unref_free(obj->listeners,(belle_sip_object_destroy_notify_t)belle_sip_channel_remove_listener,obj);
58 59
}

60 61
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_channel_t);

62 63 64
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_channel_t)=
{
	{
Simon Morlat's avatar
Simon Morlat committed
65
		BELLE_SIP_VPTR_INIT(belle_sip_channel_t,belle_sip_source_t,FALSE),
66 67 68 69 70
		(belle_sip_object_destroy_t)belle_sip_channel_destroy,
		NULL, /*clone*/
		NULL, /*marshall*/
	}
};
jehan's avatar
jehan committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
static void fix_incoming_via(belle_sip_request_t *msg, const struct addrinfo* origin){
	char received[NI_MAXHOST];
	char rport[NI_MAXSERV];
	belle_sip_header_via_t *via;
	int err=getnameinfo(origin->ai_addr,origin->ai_addrlen,received,sizeof(received),
	                rport,sizeof(rport),NI_NUMERICHOST|NI_NUMERICSERV);
	if (err!=0){
		belle_sip_error("fix_via: getnameinfo() failed: %s",gai_strerror(errno));
		return;
	}
	via=BELLE_SIP_HEADER_VIA(belle_sip_message_get_header((belle_sip_message_t*)msg,"via"));
	if (via){
		belle_sip_header_via_set_received(via,received);
		belle_sip_header_via_set_rport(via,atoi(rport));
	}
}
jehan's avatar
jehan committed
87 88 89 90 91 92 93 94 95 96 97 98 99
static int get_message_start_pos(char *buff, size_t bufflen) {
	/*FIXME still to optimize an better tested, specially REQUEST PATH and error path*/
	int i;
	int res=0;
	int status_code;
	char method[16];
	char saved_char1;

	int saved_char1_index;
	for(i=0; i<bufflen-12;i++) { /*9=strlen( SIP/2.0\r\n)*/
		saved_char1_index=bufflen-1;
		saved_char1=buff[saved_char1_index]; /*make sure buff is null terminated*/
		buff[saved_char1_index]='\0';
jehan's avatar
fix tag  
jehan committed
100 101
		res=sscanf(buff+i,"SIP/2.0 %d ",&status_code);
		if (res!=1) {
102
			res=sscanf(buff+i,"%16s %*s SIP/2.0 ",method);
jehan's avatar
jehan committed
103
		}
jehan's avatar
fix tag  
jehan committed
104 105
		buff[saved_char1_index]=saved_char1;
		if (res==1) return i;
jehan's avatar
jehan committed
106
	}
jehan's avatar
jehan committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	return -1;
}

static void belle_sip_channel_input_stream_reset(belle_sip_channel_input_stream_t* input_stream,int message_size) {
	int message_residu=0;
	if (message_size>0 && input_stream->write_ptr-input_stream->read_ptr>message_size) {
		/*still message available, copy a beginning of stream ?*/
		message_residu = input_stream->write_ptr-input_stream->read_ptr - message_size;
		memcpy(input_stream->buff
				,input_stream->read_ptr+message_size,
				message_residu);

	}
	input_stream->read_ptr=input_stream->write_ptr=input_stream->buff;
	input_stream->write_ptr+=message_residu;

	input_stream->state=WAITING_MESSAGE_START;
	input_stream->msg=NULL;
}
static size_t belle_sip_channel_input_stream_get_buff_lenght(belle_sip_channel_input_stream_t* input_stream) {
	return MAX_CHANNEL_BUFF_SIZE - (input_stream->write_ptr-input_stream->read_ptr);
}

void belle_sip_channel_process_data(belle_sip_channel_t *obj,unsigned int revents){
	int num;
	int offset;
	int i;
jehan's avatar
jehan committed
134
	size_t message_size=0;
jehan's avatar
jehan committed
135 136 137
	belle_sip_header_content_length_t* content_length_header;
	int content_length;

jehan's avatar
jehan committed
138 139 140 141 142
	if (revents)
		num=belle_sip_channel_recv(obj,obj->input_stream.write_ptr,belle_sip_channel_input_stream_get_buff_lenght(&obj->input_stream)-1);
	else
		num=obj->input_stream.write_ptr-obj->input_stream.read_ptr;

jehan's avatar
jehan committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	if (num>0){
		/*first null terminate the buff*/
		obj->input_stream.write_ptr[num]='\0';
		obj->input_stream.write_ptr+=num;

		if (obj->input_stream.state == WAITING_MESSAGE_START) {
			/*search for request*/
			if ((offset=get_message_start_pos(obj->input_stream.read_ptr,num)) >=0 ) {
				/*message found !*/
				if (offset>0) {
					belle_sip_warning("trashing [%i] bytes in frot of sip message on channel [%p]",offset,obj);
					obj->input_stream.read_ptr+=offset;
				}
				obj->input_stream.state=MESSAGE_AQUISITION;
			} else {
				belle_sip_warning("Unexpected [%s] received on channel [%p], trashing",obj->input_stream.write_ptr,obj);
				belle_sip_channel_input_stream_reset(&obj->input_stream,0);
			}
		}

		if (obj->input_stream.state==MESSAGE_AQUISITION) {
			/*search for \r\n\r\n*/
			for (i=0;i<obj->input_stream.write_ptr-obj->input_stream.read_ptr;i++) {
				if (strncmp("\r\n\r\n",&obj->input_stream.read_ptr[i],4)==0) {
					/*end of message found*/
					belle_sip_message("read message from %s:%i\n%s",obj->peer_name,obj->peer_port,obj->input_stream.read_ptr);
					obj->input_stream.msg=belle_sip_message_parse_raw(obj->input_stream.read_ptr
											,obj->input_stream.write_ptr-obj->input_stream.read_ptr
											,&message_size);
					if (obj->input_stream.msg){
						if (belle_sip_message_is_request(obj->input_stream.msg)) fix_incoming_via(BELLE_SIP_REQUEST(obj->input_stream.msg),obj->peer);
						/*check for body*/
						if ((content_length_header = (belle_sip_header_content_length_t*)belle_sip_message_get_header(obj->input_stream.msg,BELLE_SIP_CONTENT_LENGTH)) != NULL
								&& belle_sip_header_content_length_get_content_length(content_length_header)>0) {
							obj->input_stream.read_ptr+=message_size;
							obj->input_stream.state=BODY_AQUISITION;
						} else {
							/*no body*/
							goto message_ready;
						}

					}else{
						belle_sip_error("Could not parse [%s], resetting channel [%p]",obj->input_stream.read_ptr,obj);
						belle_sip_channel_input_stream_reset(&obj->input_stream,0);
					}
				}
			}
		}

		if (obj->input_stream.state==BODY_AQUISITION) {
			content_length=belle_sip_header_content_length_get_content_length((belle_sip_header_content_length_t*)belle_sip_message_get_header(obj->input_stream.msg,BELLE_SIP_CONTENT_LENGTH));
jehan's avatar
jehan committed
194
			if (content_length <= obj->input_stream.write_ptr-obj->input_stream.read_ptr) {
jehan's avatar
jehan committed
195
				/*great body completed*/
jehan's avatar
jehan committed
196
				belle_sip_message("read body from %s:%i\n%s",obj->peer_name,obj->peer_port,obj->input_stream.read_ptr);
jehan's avatar
jehan committed
197
				belle_sip_message_set_body(obj->input_stream.msg,obj->input_stream.read_ptr,content_length);
198
				obj->input_stream.read_ptr+=content_length;
jehan's avatar
jehan committed
199 200 201 202
				goto message_ready;

			}
		}
Simon Morlat's avatar
Simon Morlat committed
203 204 205 206 207 208 209
		return;
	message_ready:
		obj->incoming_messages=belle_sip_list_append(obj->incoming_messages,obj->input_stream.msg);
		belle_sip_channel_input_stream_reset(&obj->input_stream,message_size);
		BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(obj->listeners,belle_sip_channel_listener_t,on_event,obj,revents);
		if (obj->input_stream.write_ptr-obj->input_stream.read_ptr>0) {
			/*process residu*/
jehan's avatar
jehan committed
210
			belle_sip_channel_process_data(obj,0);
Simon Morlat's avatar
Simon Morlat committed
211 212
		}
		return;
jehan's avatar
jehan committed
213 214
	} else if (num == 0) {
		channel_set_state(obj,BELLE_SIP_CHANNEL_DISCONNECTED);
215
		belle_sip_channel_close(obj);
jehan's avatar
jehan committed
216 217
	} else {
		belle_sip_error("Receive error on channel [%p]",obj);
jehan's avatar
jehan committed
218
		channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
219
		belle_sip_channel_close(obj);
jehan's avatar
jehan committed
220 221
	}
	return;
Simon Morlat's avatar
Simon Morlat committed
222
}
223

jehan's avatar
jehan committed
224

jehan's avatar
jehan committed
225
void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_stack_t *stack, int fd, belle_sip_source_func_t process_data,const char *bindip,int localport,const char *peername, int peer_port){
226 227 228
	obj->peer_name=belle_sip_strdup(peername);
	obj->peer_port=peer_port;
	obj->peer=NULL;
229
	obj->stack=stack;
230 231 232
	if (strcmp(bindip,"::0")!=0 && strcmp(bindip,"0.0.0.0")!=0)
		obj->local_ip=belle_sip_strdup(bindip);
	obj->local_port=localport;
jehan's avatar
jehan committed
233

jehan's avatar
jehan committed
234 235 236
	if (process_data) {
		belle_sip_fd_source_init((belle_sip_source_t*)obj,(belle_sip_source_func_t)process_data,obj,fd,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_ERROR,-1);
	}
jehan's avatar
jehan committed
237
	belle_sip_channel_input_stream_reset(&obj->input_stream,0);
238 239
}

240 241 242 243 244 245 246 247 248 249 250
void belle_sip_channel_add_listener(belle_sip_channel_t *obj, belle_sip_channel_listener_t *l){
	obj->listeners=belle_sip_list_append(obj->listeners,
	                belle_sip_object_weak_ref(l,
	                (belle_sip_object_destroy_notify_t)belle_sip_channel_remove_listener,obj));
}

void belle_sip_channel_remove_listener(belle_sip_channel_t *obj, belle_sip_channel_listener_t *l){
	belle_sip_object_weak_unref(l,(belle_sip_object_destroy_notify_t)belle_sip_channel_remove_listener,obj);
	obj->listeners=belle_sip_list_remove(obj->listeners,l);
}

Simon Morlat's avatar
Simon Morlat committed
251 252
int belle_sip_channel_matches(const belle_sip_channel_t *obj, const char *peername, int peerport, const struct addrinfo *addr){
	if (peername && strcmp(peername,obj->peer_name)==0 && peerport==obj->peer_port)
253 254 255 256 257 258
		return 1;
	if (addr && obj->peer) 
		return addr->ai_addrlen==obj->peer->ai_addrlen && memcmp(addr->ai_addr,obj->peer->ai_addr,addr->ai_addrlen)==0;
	return 0;
}

259 260 261 262 263
const char *belle_sip_channel_get_local_address(belle_sip_channel_t *obj, int *port){
	if (port) *port=obj->local_port;
	return obj->local_ip;
}

264 265 266
int belle_sip_channel_is_reliable(const belle_sip_channel_t *obj){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->reliable;
}
267

jehan's avatar
jehan committed
268 269 270 271 272 273 274 275 276 277 278
const char * belle_sip_channel_get_transport_name_lower_case(const belle_sip_channel_t *obj){
	const char* transport = belle_sip_channel_get_transport_name(obj);
	if (strcasecmp("udp",transport)==0) return "udp";
	else if (strcasecmp("tcp",transport)==0) return "tcp";
	else if (strcasecmp("tls",transport)==0) return "tls";
	else if (strcasecmp("dtls",transport)==0) return "dtls";
	else {
		belle_sip_message("Cannot convert [%s] to lower case",transport);
		return transport;
	}
}
279

280
const char * belle_sip_channel_get_transport_name(const belle_sip_channel_t *obj){
281
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->transport;
282 283 284 285 286 287 288 289 290 291
}

int belle_sip_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->channel_send(obj,buf,buflen);
}

int belle_sip_channel_recv(belle_sip_channel_t *obj, void *buf, size_t buflen){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->channel_recv(obj,buf,buflen);
}

292 293 294 295
void belle_sip_channel_close(belle_sip_channel_t *obj){
	BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->close(obj);
}

296 297 298 299
const struct addrinfo * belle_sip_channel_get_peer(belle_sip_channel_t *obj){
	return obj->peer;
}

jehan's avatar
jehan committed
300
belle_sip_message_t* belle_sip_channel_pick_message(belle_sip_channel_t *obj) {
jehan's avatar
jehan committed
301 302 303 304 305 306 307 308 309
	belle_sip_message_t* result=NULL;
	belle_sip_list_t* front;
	if ((front=obj->incoming_messages)==NULL) {
		belle_sip_error("Cannot pickup incoming message, empty list");
	} else {
		result = (belle_sip_message_t*)obj->incoming_messages->data;
		obj->incoming_messages=belle_sip_list_remove_link(obj->incoming_messages,obj->incoming_messages);
		belle_sip_free(front);
	}
jehan's avatar
jehan committed
310 311
	return result;
}
312

jehan's avatar
jehan committed
313
void channel_set_state(belle_sip_channel_t *obj, belle_sip_channel_state_t state) {
Simon Morlat's avatar
Simon Morlat committed
314
	belle_sip_message("channel %p: state %s",obj,belle_sip_channel_state_to_string(state));
315
	obj->state=state;
316
	BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(obj->listeners,belle_sip_channel_listener_t,on_state_changed,obj,state);
317
}
318

Simon Morlat's avatar
Simon Morlat committed
319 320

static void _send_message(belle_sip_channel_t *obj, belle_sip_message_t *msg){
321
	char buffer[belle_sip_network_buffer_size];
322 323 324
	int len;
	BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(obj->listeners,belle_sip_channel_listener_t,on_sending,obj,msg);
	len=belle_sip_object_marshal((belle_sip_object_t*)msg,buffer,0,sizeof(buffer));
325 326 327
	if (len>0){
		int ret=belle_sip_channel_send(obj,buffer,len);
		if (ret==-1){
328
			channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
329
			belle_sip_channel_close(obj);
330 331
		}else{
			belle_sip_message("channel %p: message sent: \n%s",obj,buffer);
332 333 334 335
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
336 337 338 339 340 341 342 343
/* just to emulate network transmission delay */

typedef struct delayed_send{
	belle_sip_channel_t *chan;
	belle_sip_message_t *msg;
}delayed_send_t;

static int on_delayed_send_do(delayed_send_t *ds){
344
	belle_sip_message("on_delayed_send_do(): sending now");
Simon Morlat's avatar
Simon Morlat committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	if (ds->chan->state==BELLE_SIP_CHANNEL_READY){
		_send_message(ds->chan,ds->msg);
	}
	belle_sip_object_unref(ds->chan);
	belle_sip_object_unref(ds->msg);
	belle_sip_free(ds);
	return FALSE;
}

static void send_message(belle_sip_channel_t *obj, belle_sip_message_t *msg){
	if (obj->stack->tx_delay>0){
		delayed_send_t *ds=belle_sip_new(delayed_send_t);
		ds->chan=(belle_sip_channel_t*)belle_sip_object_ref(obj);
		ds->msg=(belle_sip_message_t*)belle_sip_object_ref(msg);
		belle_sip_main_loop_add_timeout(obj->stack->ml,(belle_sip_source_func_t)on_delayed_send_do,ds,obj->stack->tx_delay);
360
		belle_sip_message("channel %p: message sending delayed by %i ms",obj,obj->stack->tx_delay);
Simon Morlat's avatar
Simon Morlat committed
361 362
	}else _send_message(obj,msg);
}
363

Simon Morlat's avatar
Simon Morlat committed
364 365 366 367 368
void belle_sip_channel_prepare(belle_sip_channel_t *obj){
	obj->prepare=1;
	channel_process_queue(obj);
}

jehan's avatar
jehan committed
369
void channel_process_queue(belle_sip_channel_t *obj){
Simon Morlat's avatar
Simon Morlat committed
370 371 372 373 374 375 376 377 378
	switch(obj->state){
		case BELLE_SIP_CHANNEL_INIT:
			if (obj->prepare) belle_sip_channel_resolve(obj);
		break;
		case BELLE_SIP_CHANNEL_RES_DONE:
			if (obj->prepare) belle_sip_channel_connect(obj);
		break;
		case BELLE_SIP_CHANNEL_READY:
			if (obj->msg) {
379 380 381
				send_message(obj, obj->msg);
				belle_sip_object_unref(obj->msg);
				obj->msg=NULL;
Simon Morlat's avatar
Simon Morlat committed
382
			}
383
			break;
Simon Morlat's avatar
Simon Morlat committed
384 385 386 387 388 389 390 391
		case BELLE_SIP_CHANNEL_ERROR:
			if (obj->msg){
				belle_sip_object_unref(obj->msg);
				obj->msg=NULL;
			}
		break;
		default:
		break;
392 393 394
	}
}

jehan's avatar
jehan committed
395
void belle_sip_channel_set_ready(belle_sip_channel_t *obj, const struct sockaddr *addr, socklen_t slen){
396 397 398 399 400 401 402 403 404 405 406 407 408 409
	char name[NI_MAXHOST];
	char serv[NI_MAXSERV];

	if (obj->local_ip==NULL){
		int err=getnameinfo(addr,slen,name,sizeof(name),serv,sizeof(serv),NI_NUMERICHOST|NI_NUMERICSERV);
		if (err!=0){
			belle_sip_error("belle_sip_channel_connect(): getnameinfo() failed: %s",gai_strerror(err));
		}else{
			obj->local_ip=belle_sip_strdup(name);
			obj->local_port=atoi(serv);
			belle_sip_message("Channel has local address %s:%s",name,serv);
		}
	}
	channel_set_state(obj,BELLE_SIP_CHANNEL_READY);
Simon Morlat's avatar
Simon Morlat committed
410
	obj->prepare=0;
411 412 413
	channel_process_queue(obj);
}

414 415 416 417 418
static void channel_res_done(void *data, const char *name, struct addrinfo *res){
	belle_sip_channel_t *obj=(belle_sip_channel_t*)data;
	obj->resolver_id=0;
	if (res){
		obj->peer=res;
419
		channel_set_state(obj,BELLE_SIP_CHANNEL_RES_DONE);
420
	}else{
421
		channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
422 423 424 425
	}
	channel_process_queue(obj);
}

jehan's avatar
jehan committed
426
void belle_sip_channel_resolve(belle_sip_channel_t *obj){
427 428
	channel_set_state(obj,BELLE_SIP_CHANNEL_RES_IN_PROGRESS);
	obj->resolver_id=belle_sip_resolve(obj->peer_name, obj->peer_port, 0, channel_res_done, obj, obj->stack->ml);
jehan's avatar
jehan committed
429
	return ;
430 431
}

jehan's avatar
jehan committed
432 433 434
void belle_sip_channel_connect(belle_sip_channel_t *obj){
	channel_set_state(obj,BELLE_SIP_CHANNEL_CONNECTING);
	if(BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->connect(obj,obj->peer->ai_addr,obj->peer->ai_addrlen)) {
Simon Morlat's avatar
Simon Morlat committed
435
		belle_sip_error("Cannot connect to [%s://%s:%i]",belle_sip_channel_get_transport_name(obj),obj->peer_name,obj->peer_port);
jehan's avatar
jehan committed
436 437 438 439
		channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
		channel_process_queue(obj);
	}
	return;
440 441 442
}

int belle_sip_channel_queue_message(belle_sip_channel_t *obj, belle_sip_message_t *msg){
443
	if (obj->msg!=NULL){
Simon Morlat's avatar
Simon Morlat committed
444
		belle_sip_error("Queue is not a queue, state=%s", belle_sip_channel_state_to_string(obj->state));
445 446 447
		return -1;
	}
	obj->msg=(belle_sip_message_t*)belle_sip_object_ref(msg);
Simon Morlat's avatar
Simon Morlat committed
448 449
	if (obj->state==BELLE_SIP_CHANNEL_INIT)
		belle_sip_channel_prepare(obj);
450 451 452 453 454 455
	channel_process_queue(obj);
	return 0;
}