channel.c 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 19 20
    (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"
21
#include <limits.h>
22

23 24
static void channel_prepare_continue(belle_sip_channel_t *obj);
static void channel_process_queue(belle_sip_channel_t *obj);
25 26 27 28 29
static void channel_begin_send_background_task(belle_sip_channel_t *obj);
static void channel_end_send_background_task(belle_sip_channel_t *obj);
static void channel_begin_recv_background_task(belle_sip_channel_t *obj);
static void channel_end_recv_background_task(belle_sip_channel_t *obj);

30

Simon Morlat's avatar
Simon Morlat committed
31
const char *belle_sip_channel_state_to_string(belle_sip_channel_state_t state){
32 33 34 35 36 37 38 39 40
	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";
41 42
		case BELLE_SIP_CHANNEL_RETRY:
			return "RETRY";
43 44 45 46
		case BELLE_SIP_CHANNEL_READY:
			return "READY";
		case BELLE_SIP_CHANNEL_ERROR:
			return "ERROR";
jehan's avatar
jehan committed
47
		case BELLE_SIP_CHANNEL_DISCONNECTED:
48
			return "DISCONNECTED";
49 50
	}
	return "BAD";
51
}
52

53 54 55 56 57 58 59 60 61 62
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;
}

63
static void belle_sip_channel_destroy(belle_sip_channel_t *obj){
64
	if (obj->peer_list) freeaddrinfo(obj->peer_list);
65
	if (obj->peer_cname) belle_sip_free(obj->peer_cname);
66
	belle_sip_free(obj->peer_name);
67
	if (obj->local_ip) belle_sip_free(obj->local_ip);
68
	obj->listeners=for_each_weak_unref_free(obj->listeners,(belle_sip_object_destroy_notify_t)belle_sip_channel_remove_listener,obj);
69
	if (obj->resolver_ctx>0) belle_sip_resolver_context_cancel(obj->resolver_ctx);
70 71 72 73
	if (obj->inactivity_timer){
		belle_sip_main_loop_remove_source(obj->stack->ml,obj->inactivity_timer);
		belle_sip_object_unref(obj->inactivity_timer);
	}
74
	if (obj->public_ip) belle_sip_free(obj->public_ip);
75 76 77
	channel_end_send_background_task(obj);
	channel_end_recv_background_task(obj);
	/*normally this should do nothing because it sould have been terminated already,
78
		however leaving a background task open is so dangerous that we have to be paranoid*/
79
	belle_sip_message("Channel [%p] destroyed",obj);
80 81
}

82 83
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_channel_t);

84
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_channel_t)
85
	{
Simon Morlat's avatar
Simon Morlat committed
86
		BELLE_SIP_VPTR_INIT(belle_sip_channel_t,belle_sip_source_t,FALSE),
87 88
		(belle_sip_object_destroy_t)belle_sip_channel_destroy,
		NULL, /*clone*/
89
		NULL, /*marshal*/
90
	}
91
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
92

jehan's avatar
jehan committed
93 94 95 96
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;
Ghislain MARY's avatar
Ghislain MARY committed
97
	int err;
98 99 100
	struct sockaddr_storage saddr;
	socklen_t slen=sizeof(saddr);
	
jehan's avatar
jehan committed
101 102 103 104
	if (!origin) {
		belle_sip_warning("cannot fix via for message [%p], probably a test",msg);
		return;
	}
105 106
	belle_sip_address_remove_v4_mapping(origin->ai_addr, (struct sockaddr*)&saddr, &slen);
	err=getnameinfo((struct sockaddr*)&saddr,slen,received,sizeof(received),
jehan's avatar
jehan committed
107 108 109 110 111 112 113
	                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){
114 115 116 117 118 119 120 121 122 123
		const char* host = belle_sip_header_via_get_host(via);
		
		if (strcmp(host,received)!=0)
				belle_sip_header_via_set_received(via,received);
			
		if (belle_sip_parameters_has_parameter(BELLE_SIP_PARAMETERS(via),"rport")){
			int port = belle_sip_header_via_get_listening_port(via);
			int rport_int=atoi(rport);
			if (rport_int!=port) belle_sip_header_via_set_rport(via,atoi(rport));
		}
jehan's avatar
jehan committed
124 125
	}
}
126

jehan's avatar
jehan committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
/*token       =  1*(alphanum / "-" / "." / "!" / "%" / "*"
                     / "_" / "+" / "`" / "'" / "~" )
 *
 * */
static int is_token(const char* buff,size_t bufflen ) {
	int i;
	for (i=0; i<bufflen && buff[i]!='\0';i++) {
		switch(buff[i]) {
			case '-' :
			case '.' :
			case '!' :
			case '%' :
			case '*' :
			case '_' :
			case '+' :
			case '`' :
			case '\'' :
			case '~' :
				break;
		default:
			if ((buff[i]>='0' && buff[i]<='9')
				|| (buff[i]>='A' && buff[i]<='Z')
				|| (buff[i]>='a' && buff[i]<='z')
				|| (buff[i]=='\0'))
				continue;
			else
				return 0;
		}
	}
	return 1;
}
jehan's avatar
jehan committed
158
static int get_message_start_pos(char *buff, size_t bufflen) {
Simon Morlat's avatar
Simon Morlat committed
159
	/*FIXME still to optimize and better test, specially REQUEST PATH and error path*/
jehan's avatar
jehan committed
160 161 162
	int i;
	int res=0;
	int status_code;
Simon Morlat's avatar
Simon Morlat committed
163
	char method[17]={0};
jehan's avatar
jehan committed
164
	char saved_char1;
Simon Morlat's avatar
Simon Morlat committed
165
	char sip_version[10]={0};
jehan's avatar
jehan committed
166
	int saved_char1_index;
167

168
	for(i=0; i<(int)bufflen-12;i++) { /*9=strlen( SIP/2.0\r\n)*/
jehan's avatar
jehan committed
169 170 171
		switch (buff[i]) { /*to avoid this character to be ignored by scanf*/
			case '\r':
			case '\n':
172 173
			case ' ' :
			case '\t':
jehan's avatar
jehan committed
174 175 176 177
				continue;
			default:
				break;
		}
jehan's avatar
jehan committed
178 179 180
		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
181
		res=sscanf(buff+i,"SIP/2.0 %d ",&status_code);
jehan's avatar
jehan committed
182
		if (res!=1) res=sscanf(buff+i,"HTTP/1.%*i %d ",&status_code); /*might be HTTP ?*/
jehan's avatar
fix tag  
jehan committed
183
		if (res!=1) {
184
			res= sscanf(buff+i,"%16s %*s %9s\r\n",method,sip_version)==2
jehan's avatar
jehan committed
185
					&& is_token(method,sizeof(method))
Simon Morlat's avatar
Simon Morlat committed
186
					&& (strcmp("SIP/2.0",sip_version)==0 || strncmp("HTTP/1.",sip_version,strlen("HTTP/1."))==0);
jehan's avatar
jehan committed
187
		}
jehan's avatar
fix tag  
jehan committed
188 189
		buff[saved_char1_index]=saved_char1;
		if (res==1) return i;
jehan's avatar
jehan committed
190
	}
jehan's avatar
jehan committed
191 192 193
	return -1;
}

194
static void belle_sip_channel_input_stream_reset(belle_sip_channel_input_stream_t* input_stream) {
195 196 197 198 199 200 201 202
	int remaining;
	
	remaining=input_stream->write_ptr-input_stream->read_ptr;
	if (remaining>0){
		/* copy remaning bytes at top of buffer*/
		memmove(input_stream->buff,input_stream->read_ptr,remaining);
		input_stream->read_ptr=input_stream->buff;
		input_stream->write_ptr=input_stream->buff+remaining;
203
		*input_stream->write_ptr='\0';
204 205 206
	}else{
		input_stream->read_ptr=input_stream->write_ptr=input_stream->buff;
	}
jehan's avatar
jehan committed
207 208
	input_stream->state=WAITING_MESSAGE_START;
	input_stream->msg=NULL;
209 210 211 212 213 214
	input_stream->chuncked_mode=FALSE;
	input_stream->content_length=-1;
	if (input_stream->body){
		belle_sip_free(input_stream->body);
		input_stream->body=NULL;
	}
jehan's avatar
jehan committed
215
}
216

217
static size_t belle_sip_channel_input_stream_get_buff_length(belle_sip_channel_input_stream_t* input_stream) {
jehan's avatar
jehan committed
218
	return MAX_CHANNEL_BUFF_SIZE - (input_stream->write_ptr-input_stream->buff);
jehan's avatar
jehan committed
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
void belle_sip_channel_set_public_ip_port(belle_sip_channel_t *obj, const char *public_ip, int port){
	if (obj->public_ip){
		int ip_changed=0;
		int port_changed=0;
		
		if (public_ip && strcmp(obj->public_ip,public_ip)!=0){
			ip_changed=1;
		}
		if (port!=obj->public_port){
			port_changed=1;
		}
		if (ip_changed || port_changed){
			belle_sip_warning("channel [%p]: public ip is changed from [%s:%i] to [%s:%i]",obj,obj->public_ip,obj->public_port,public_ip,port);
		}
		belle_sip_free(obj->public_ip);
		obj->public_ip=NULL;
	}else if (public_ip){
		belle_sip_message("channel [%p]: discovered public ip and port are [%s:%i]",obj,public_ip,port);
	}
	if (public_ip){
		obj->public_ip=belle_sip_strdup(public_ip);
	}
	obj->public_port=port;
}

static void belle_sip_channel_learn_public_ip_port(belle_sip_channel_t *obj, belle_sip_response_t *resp){
	belle_sip_header_via_t *via=belle_sip_message_get_header_by_type(resp,belle_sip_header_via_t);
	const char *received;
	int rport;
	
	if (!via){
		belle_sip_error("channel [%p]: no via in response.",obj);
		return;
	}
255 256 257 258 259

	if (!(received=belle_sip_header_via_get_received(via))) {
		/*use address from via*/;
		received=belle_sip_header_via_get_host(via);
	}
260
	
261 262 263 264
	rport=belle_sip_header_via_get_rport(via);
	if (rport<=0){
		/* no rport, the via port might be good then*/
		rport=belle_sip_header_via_get_listening_port(via);
265
	}
266 267
	belle_sip_channel_set_public_ip_port(obj,received,rport);

268
	obj->learnt_ip_port=TRUE;
269 270
}

271
static void belle_sip_channel_message_ready(belle_sip_channel_t *obj){
272 273 274
	belle_sip_message_t *msg=obj->input_stream.msg;
	if (belle_sip_message_is_response(msg)) belle_sip_channel_learn_public_ip_port(obj,BELLE_SIP_RESPONSE(msg));
	obj->incoming_messages=belle_sip_list_append(obj->incoming_messages,msg);
275
	belle_sip_channel_input_stream_reset(&obj->input_stream);
276 277
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/*returns TRUE if a body is expected, and initialize a few things in the input stream context*/
static int check_body(belle_sip_channel_t *obj){
	belle_sip_message_t *msg=obj->input_stream.msg;
	belle_sip_header_content_length_t* content_length_header = belle_sip_message_get_header_by_type(msg,belle_sip_header_content_length_t);
	obj->input_stream.content_length= content_length_header ? belle_sip_header_content_length_get_content_length(content_length_header) : 0;
	
	if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(msg,belle_sip_response_t) || BELLE_SIP_OBJECT_IS_INSTANCE_OF(msg,belle_sip_request_t)){
		return obj->input_stream.content_length>0;
	}else{/*http*/
		if (belle_sip_message_get_header_by_type(msg, belle_sip_header_content_type_t)!=NULL){
			belle_sip_header_t *transfer_encoding=belle_sip_message_get_header(msg,"Transfer-Encoding");
			
			if (transfer_encoding){
				const char *value=belle_sip_header_get_unparsed_value(transfer_encoding);
				if (strstr(value,"chunked")!=0){
					obj->input_stream.chuncked_mode=1;
					obj->input_stream.content_length=0;
					obj->input_stream.chunk_size=-1;
				}
			}
			return TRUE;
		}
300 301 302 303
	}
	return FALSE;
}

304 305
static int acquire_body_simple(belle_sip_channel_t *obj, int end_of_stream){
	int content_length=obj->input_stream.content_length;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	
	if (end_of_stream && content_length==-1){
		content_length=obj->input_stream.write_ptr-obj->input_stream.read_ptr;
	}
	if (content_length!=-1 && content_length <= obj->input_stream.write_ptr-obj->input_stream.read_ptr){
		/*great body completed
		belle_sip_message("channel [%p] read [%i] bytes of body from %s:%i\n%s"	,obj
			,content_length
			,obj->peer_name
			,obj->peer_port
			,obj->input_stream.read_ptr);*/
		belle_sip_message_set_body(obj->input_stream.msg,obj->input_stream.read_ptr,content_length);
		obj->input_stream.read_ptr+=content_length;
		belle_sip_channel_message_ready(obj);
		return BELLE_SIP_CONTINUE;
	}else{
		/*body is not finished, we need more data*/
		return BELLE_SIP_STOP;
	}
}

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
static int acquire_chuncked_body(belle_sip_channel_t *obj, int end_of_stream){
	belle_sip_channel_input_stream_t *st=&obj->input_stream;
	do{
		if (st->chunk_size==-1){
			char *tmp;
			belle_sip_message("seeing: %s",st->read_ptr);
			while ( (tmp=strstr(st->read_ptr,"\r\n"))==st->read_ptr){/*skip \r\n*/
				st->read_ptr+=2;
			}
			
			if (tmp!=NULL){
				/*the chunk length is there*/
				long chunksize=strtol(st->read_ptr,NULL,16);
				if (chunksize>=0 && chunksize!=LONG_MAX){
					if (chunksize==0){
						belle_sip_message("Got end of chunked body");
						belle_sip_message_assign_body(obj->input_stream.msg,(char*)st->body,st->content_length);
						st->body=NULL;
						st->read_ptr=tmp+4; /*last chunk indicator finishes with two \r\n*/
						if (st->read_ptr>st->write_ptr) st->read_ptr=st->write_ptr;
						belle_sip_channel_message_ready(obj);
						return BELLE_SIP_CONTINUE;
					}else{
						belle_sip_message("Getting chunk of %i bytes",(int)chunksize);
						st->chunk_size=chunksize;
						st->read_ptr=tmp+2;
					}
				}else{
					belle_sip_error("Chunk parse error");
					belle_sip_channel_input_stream_reset(st);
					return BELLE_SIP_CONTINUE;
				}
			}else{
				/*need more data*/
				return BELLE_SIP_STOP;
			}
		}
		if (st->chunk_size<=st->write_ptr-st->read_ptr){
			/*we have a chunk completed*/
			int prev_ctlen=st->content_length;
			st->content_length+=st->chunk_size;
			st->body=belle_sip_realloc(st->body,st->content_length);
			memcpy(st->body+prev_ctlen,st->read_ptr,st->chunk_size);
			st->read_ptr+=st->chunk_size;
			belle_sip_message("Chunk of [%i] bytes completed",st->chunk_size);
			st->chunk_size=-1;/*wait for next chunk indicator*/
		}else{
			/*need more data*/
			return BELLE_SIP_STOP;
		}
	}while(1);
}

static int acquire_body(belle_sip_channel_t *obj, int end_of_stream){
	if (obj->input_stream.chuncked_mode)
		return acquire_chuncked_body(obj,end_of_stream);
	else return acquire_body_simple(obj,end_of_stream);
}

386
void belle_sip_channel_parse_stream(belle_sip_channel_t *obj, int end_of_stream){
jehan's avatar
jehan committed
387
	int offset;
388
	size_t read_size=0;
389 390 391 392
	int num;
	
	while ((num=(obj->input_stream.write_ptr-obj->input_stream.read_ptr))>0){
	
jehan's avatar
jehan committed
393
		if (obj->input_stream.state == WAITING_MESSAGE_START) {
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
			int i;
			/*first, make sure there is \r\n in the buffer, otherwise, micro parser cannot conclude, because we need a complete request or response line somewhere*/
			for (i=0;i<num-1;i++) {
				if ((obj->input_stream.read_ptr[i]=='\r' && obj->input_stream.read_ptr[i+1]=='\n')
						|| belle_sip_channel_input_stream_get_buff_length(&obj->input_stream) <= 1 /*1 because null terminated*/  /*if buffer full try to parse in any case*/) {
					/*good, now we can start searching  for request/response*/
					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 front of sip message on channel [%p]",offset,obj);
							obj->input_stream.read_ptr+=offset;
						}
						obj->input_stream.state=MESSAGE_AQUISITION;
					} else {
						belle_sip_debug("Unexpected [%s] received on channel [%p], trashing",obj->input_stream.read_ptr,obj);
						obj->input_stream.read_ptr=obj->input_stream.write_ptr;
						belle_sip_channel_input_stream_reset(&obj->input_stream);
						continue;
					}
				break;
jehan's avatar
jehan committed
414
				}
415 416 417 418 419
			}

			if (i >= num-1) {
				belle_sip_debug("[%s] received on channel [%p], cannot determine if expected or not, waiting for new data",obj->input_stream.read_ptr,obj);
				break;
jehan's avatar
jehan committed
420 421 422 423 424
			}
		}

		if (obj->input_stream.state==MESSAGE_AQUISITION) {
			/*search for \r\n\r\n*/
jehan's avatar
jehan committed
425 426
			char* end_of_message=NULL;
			if ((end_of_message=strstr(obj->input_stream.read_ptr,"\r\n\r\n"))){
427 428
				int bytes_to_parse;
				char tmp;
429
				/*end of message found*/
jehan's avatar
jehan committed
430
				end_of_message+=4;/*add \r\n\r\n*/
431 432 433
				bytes_to_parse=end_of_message-obj->input_stream.read_ptr;
				tmp=*end_of_message;
				*end_of_message='\0';/*this is in order for the following log to print the message only to its end.*/
434
				/*belle_sip_message("channel [%p] read message of [%i] bytes:\n%.40s...",obj, bytes_to_parse, obj->input_stream.read_ptr);*/
435
				obj->input_stream.msg=belle_sip_message_parse_raw(obj->input_stream.read_ptr
436
										,bytes_to_parse
437
										,&read_size);
438
				*end_of_message=tmp;
439 440
				obj->input_stream.read_ptr+=read_size;
				if (obj->input_stream.msg && read_size > 0){
441
					belle_sip_message("channel [%p] [%i] bytes parsed",obj,(int)read_size);
442 443 444
					belle_sip_object_ref(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->current_peer);
					/*check for body*/
445
					
446
					if (check_body(obj)){
447 448 449 450 451
						obj->input_stream.state=BODY_AQUISITION;
					} else {
						/*no body*/
						belle_sip_channel_message_ready(obj);
						continue;
jehan's avatar
jehan committed
452
					}
453
				}else{
jehan's avatar
jehan committed
454 455 456 457 458
					belle_sip_error("Could not parse [%s], on channel [%p] skipping to [%s]",obj->input_stream.read_ptr
																							,obj
																							,end_of_message);
					obj->input_stream.read_ptr=end_of_message;
					obj->input_stream.state=WAITING_MESSAGE_START;
459
					continue;
jehan's avatar
jehan committed
460
				}
461
			}else break; /*The message isn't finished to be receive, we need more data*/
jehan's avatar
jehan committed
462 463 464
		}

		if (obj->input_stream.state==BODY_AQUISITION) {
465
			if (acquire_body(obj,end_of_stream)==BELLE_SIP_STOP) break;
jehan's avatar
jehan committed
466
		}
467
	}
468 469
	if (obj->incoming_messages)
		BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(obj->listeners,belle_sip_channel_listener_t,on_event,obj,BELLE_SIP_EVENT_READ/*always a read event*/);
470 471 472 473
}

int belle_sip_channel_process_data(belle_sip_channel_t *obj,unsigned int revents){
	int num;
474 475 476 477
	int ret=BELLE_SIP_CONTINUE;
	
	/*prevent system to suspend the process until we have finish reading everything from the socket and notified the upper layer*/
	channel_begin_recv_background_task(obj);
478 479 480
	if (revents & BELLE_SIP_EVENT_READ) {
		if (obj->simulated_recv_return>0) {
			num=belle_sip_channel_recv(obj,obj->input_stream.write_ptr,belle_sip_channel_input_stream_get_buff_length(&obj->input_stream)-1);
481
		} else {
482 483
			belle_sip_message("channel [%p]: simulating recv() returning %i",obj,obj->simulated_recv_return);
			num=obj->simulated_recv_return;
Simon Morlat's avatar
Simon Morlat committed
484
		}
485 486 487 488 489
	} else {
		belle_sip_error("Unexpected event [%i] on channel [%p]",revents,obj);
		num=-1; /*to trigger an error*/
	}
	if (num>0){
490
		char *begin=obj->input_stream.write_ptr;
491 492 493
		obj->input_stream.write_ptr+=num;
		/*first null terminate the read buff*/
		*obj->input_stream.write_ptr='\0';
Simon Morlat's avatar
Simon Morlat committed
494
		if (num>50) /*to avoid tracing server based keep alives*/
495 496 497 498 499 500 501
			belle_sip_message("channel [%p]: received [%i] new bytes from [%s://%s:%i]:\n%s",
					obj,
					num,
					belle_sip_channel_get_transport_name(obj),
					obj->peer_name,
					obj->peer_port,
					begin);
502
		belle_sip_channel_parse_stream(obj,FALSE);
jehan's avatar
jehan committed
503
	} else if (num == 0) {
504 505
		/*before closing the channel, check if there was a pending message to receive, whose body acquisition is to be finished.*/
		belle_sip_channel_parse_stream(obj,TRUE);
jehan's avatar
jehan committed
506
		channel_set_state(obj,BELLE_SIP_CHANNEL_DISCONNECTED);
507
		ret=BELLE_SIP_STOP;
508 509
	} else if ( belle_sip_error_code_is_would_block(-num)){
		belle_sip_message("EWOULDBLOCK");
510
		ret=BELLE_SIP_CONTINUE;
511
	}else{
jehan's avatar
jehan committed
512
		belle_sip_error("Receive error on channel [%p]",obj);
jehan's avatar
jehan committed
513
		channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
514
		ret=BELLE_SIP_STOP;
jehan's avatar
jehan committed
515
	}
516 517
	channel_end_recv_background_task(obj);
	return ret;
Simon Morlat's avatar
Simon Morlat committed
518
}
519

520 521
static int channel_inactive_timeout(void *data, unsigned int event){
	belle_sip_channel_t *obj=(belle_sip_channel_t *)data;
522
	belle_sip_message("Channel [%p]: inactivity timeout reached.",obj);
523 524 525 526
	channel_set_state(obj,BELLE_SIP_CHANNEL_DISCONNECTED);
	return BELLE_SIP_STOP;
}

527
static void update_inactivity_timer(belle_sip_channel_t *obj, int from_recv){
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	int inactive_timeout=belle_sip_stack_get_inactive_transport_timeout(obj->stack)*1000;
	if (inactive_timeout>0){
		if (!obj->inactivity_timer ){
			obj->inactivity_timer=belle_sip_main_loop_create_timeout(obj->stack->ml,channel_inactive_timeout,obj,inactive_timeout,"Channel inactivity timer");
		}else{
			/*restart the timer for new period*/
			belle_sip_source_set_timeout(obj->inactivity_timer,inactive_timeout);
		}
	}else{
		if (obj->inactivity_timer){
			belle_sip_main_loop_remove_source(obj->stack->ml,obj->inactivity_timer);
			belle_sip_object_unref(obj->inactivity_timer);
			obj->inactivity_timer=NULL;
		}
	}
543 544
	if (from_recv)
		obj->last_recv_time=belle_sip_time_ms();
545
}
jehan's avatar
jehan committed
546

Simon Morlat's avatar
Simon Morlat committed
547 548 549 550 551 552 553
/*constructor for channels creating an outgoing connection
 * bindip local ip address to bind on, typically 0.0.0.0 or ::0
 * locaport locaport to use for binding, can be set to 0 if port doesn't matter
 * peer_cname canonical name of remote host, used for TLS verification
 * peername peer's hostname, either ip address or DNS name
 * pee_port peer's port to connect to.
 */
554
void belle_sip_channel_init(belle_sip_channel_t *obj, belle_sip_stack_t *stack,const char *bindip,int localport,const char *peer_cname, const char *peername, int peer_port){
555 556 557 558
	/*to initialize our base class:*/
	belle_sip_channel_set_socket(obj,-1,NULL);
	
	/*then initialize members*/
Simon Morlat's avatar
Simon Morlat committed
559
	obj->ai_family=AF_INET;
560
	obj->peer_cname=peer_cname ? belle_sip_strdup(peer_cname) : NULL;
561 562
	obj->peer_name=belle_sip_strdup(peername);
	obj->peer_port=peer_port;
563
	obj->stack=stack;
Simon Morlat's avatar
Simon Morlat committed
564 565 566 567 568 569
	if (bindip){
		if (strcmp(bindip,"::0")!=0 && strcmp(bindip,"0.0.0.0")!=0)
			obj->local_ip=belle_sip_strdup(bindip);
		if (strchr(bindip,':')!=NULL)
			obj->ai_family=AF_INET6;
	}
570
	obj->local_port=localport;
Simon Morlat's avatar
Simon Morlat committed
571
	obj->simulated_recv_return=1;/*not set*/
572 573 574 575 576 577
	if (peername){
		/*check if we are given a real dns name or just an ip address*/
		struct addrinfo *ai=belle_sip_ip_address_to_addrinfo(AF_UNSPEC,peername,peer_port);
		if (ai) freeaddrinfo(ai);
		else obj->has_name=TRUE;
	}
578
	belle_sip_channel_input_stream_reset(&obj->input_stream);
579
	update_inactivity_timer(obj,FALSE);
580 581
}

Simon Morlat's avatar
Simon Morlat committed
582
/*constructor for channels created by incoming connections*/
583 584 585 586 587 588 589 590 591 592
void belle_sip_channel_init_with_addr(belle_sip_channel_t *obj, belle_sip_stack_t *stack, const struct sockaddr *peer_addr, socklen_t addrlen){
	char remoteip[64];
	struct addrinfo ai;
	int peer_port;
	
	memset(&ai,0,sizeof(ai));
	ai.ai_family=peer_addr->sa_family;
	ai.ai_addr=(struct sockaddr*)peer_addr;
	ai.ai_addrlen=addrlen;
	belle_sip_addrinfo_to_ip(&ai,remoteip,sizeof(remoteip),&peer_port);
593
	belle_sip_channel_init(obj,stack,NULL,0,NULL,remoteip,peer_port);
594
	obj->peer_list=obj->current_peer=belle_sip_ip_address_to_addrinfo(ai.ai_family, obj->peer_name,obj->peer_port);
Simon Morlat's avatar
Simon Morlat committed
595
	obj->ai_family=ai.ai_family;
596 597
}

Simon Morlat's avatar
Simon Morlat committed
598
void belle_sip_channel_set_socket(belle_sip_channel_t *obj, belle_sip_socket_t sock, belle_sip_source_func_t datafunc){
jehan's avatar
jehan committed
599 600 601 602 603 604
	belle_sip_socket_source_init((belle_sip_source_t*)obj
									, datafunc
									, obj
									, sock
									, BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_WRITE
									, -1);
605 606
}

607 608 609 610 611 612 613 614 615 616 617
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);
}

618 619 620 621
int belle_sip_channel_matches(const belle_sip_channel_t *obj, const belle_sip_hop_t *hop, const struct addrinfo *addr){
	if (hop && strcmp(hop->host,obj->peer_name)==0 && hop->port==obj->peer_port){
		if (hop->cname && obj->peer_cname && strcmp(hop->cname,obj->peer_cname)!=0)
			return 0; /*cname mismatch*/
622
		return 1;
623
	}
624
	if (addr && obj->current_peer)
625
		return addr->ai_addrlen==obj->current_peer->ai_addrlen && memcmp(addr->ai_addr,obj->current_peer->ai_addr,addr->ai_addrlen)==0;
626 627 628
	return 0;
}

629 630 631 632 633
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;
}

634 635 636
int belle_sip_channel_is_reliable(const belle_sip_channel_t *obj){
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->reliable;
}
637

jehan's avatar
jehan committed
638 639 640 641 642 643 644 645 646 647 648
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;
	}
}
649

650
const char * belle_sip_channel_get_transport_name(const belle_sip_channel_t *obj){
651
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->transport;
652 653 654
}

int belle_sip_channel_send(belle_sip_channel_t *obj, const void *buf, size_t buflen){
655
	update_inactivity_timer(obj,FALSE);
656 657 658 659
	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){
660
	update_inactivity_timer(obj,TRUE);
661 662 663
	return BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->channel_recv(obj,buf,buflen);
}

664
void belle_sip_channel_close(belle_sip_channel_t *obj){
665
	if (BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->close)
666
		BELLE_SIP_OBJECT_VPTR(obj,belle_sip_channel_t)->close(obj); /*udp channel doesn't have close function*/
667 668
	belle_sip_main_loop_remove_source(obj->stack->ml,(belle_sip_source_t*)obj);
	belle_sip_source_uninit((belle_sip_source_t*)obj);
669 670
}

671
const struct addrinfo * belle_sip_channel_get_peer(belle_sip_channel_t *obj){
672
	return obj->current_peer;
673 674
}

675 676 677
static void channel_on_send_background_task_ended(belle_sip_channel_t *obj){
	belle_sip_warning("channel [%p]: send background task has to be ended now, but work isn't finished.",obj);
	channel_end_send_background_task(obj);
678 679
}

680
static void channel_begin_send_background_task(belle_sip_channel_t *obj){
681
	if (obj->bg_task_id==0){
682 683 684
		obj->bg_task_id=belle_sip_begin_background_task("belle-sip channel in progress",(void (*)(void*))channel_on_send_background_task_ended, obj);
		if (obj->bg_task_id) belle_sip_message("channel [%p]: starting send background task with id=[%x].",obj,obj->bg_task_id);
	}
685 686
}

687
static void channel_end_send_background_task(belle_sip_channel_t *obj){
688
	if (obj->bg_task_id){
689
		belle_sip_message("channel [%p]: ending send background task with id=[%x].",obj,obj->bg_task_id);
690 691 692 693 694
		belle_sip_end_background_task(obj->bg_task_id);
		obj->bg_task_id=0;
	}
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
static void channel_on_recv_background_task_ended(belle_sip_channel_t *obj){
	belle_sip_warning("channel [%p]: recv background task has to be ended now, but work isn't finished.",obj);
	channel_end_recv_background_task(obj);
}

static void channel_begin_recv_background_task(belle_sip_channel_t *obj){
	if (obj->recv_bg_task_id==0){
		obj->recv_bg_task_id=belle_sip_begin_background_task("belle-sip channel in progress",(void (*)(void*))channel_on_recv_background_task_ended, obj);
		if (obj->recv_bg_task_id) belle_sip_message("channel [%p]: starting recv background task with id=[%x].",obj,obj->recv_bg_task_id);
	}
}

static void channel_end_recv_background_task(belle_sip_channel_t *obj){
	if (obj->recv_bg_task_id){
		belle_sip_message("channel [%p]: ending recv background task with id=[%x].",obj,obj->recv_bg_task_id);
		belle_sip_end_background_task(obj->recv_bg_task_id);
		obj->recv_bg_task_id=0;
	}
}

jehan's avatar
jehan committed
715
belle_sip_message_t* belle_sip_channel_pick_message(belle_sip_channel_t *obj) {
jehan's avatar
jehan committed
716 717
	belle_sip_message_t* result=NULL;
	belle_sip_list_t* front;
718
	if ((front=obj->incoming_messages)!=NULL) {
jehan's avatar
jehan committed
719 720 721 722
		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
723 724
	return result;
}
725

Simon Morlat's avatar
Simon Morlat committed
726
static void channel_invoke_state_listener(belle_sip_channel_t *obj){
727 728 729
	if (obj->state==BELLE_SIP_CHANNEL_DISCONNECTED || obj->state==BELLE_SIP_CHANNEL_ERROR){
		belle_sip_channel_close(obj);
	}
Simon Morlat's avatar
Simon Morlat committed
730
	BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(obj->listeners,belle_sip_channel_listener_t,on_state_changed,obj,obj->state);
Simon Morlat's avatar
Simon Morlat committed
731 732
}

733 734 735 736 737
static void channel_invoke_state_listener_defered(belle_sip_channel_t *obj){
	channel_invoke_state_listener(obj);
	belle_sip_object_unref(obj);
}

738 739 740 741 742
static void channel_connect_next(belle_sip_channel_t *obj){
	belle_sip_channel_connect(obj);
	belle_sip_object_unref(obj);
}

743
static void belle_sip_channel_handle_error(belle_sip_channel_t *obj){
744 745 746 747 748 749 750
	if (obj->state!=BELLE_SIP_CHANNEL_READY){
		/* Previous connection attempts were failed (channel could not get ready).*/
		/* See if you can retry on an alternate ip address.*/
		if (obj->current_peer && obj->current_peer->ai_next){ /*obj->current_peer may be null in case of dns error*/
			obj->current_peer=obj->current_peer->ai_next;
			channel_set_state(obj,BELLE_SIP_CHANNEL_RETRY);
			belle_sip_channel_close(obj);
751
			belle_sip_main_loop_do_later(obj->stack->ml,(belle_sip_callback_t)channel_connect_next,belle_sip_object_ref(obj));
752 753 754
			return;
		}/*else we have already tried all the ip addresses, so give up and notify the error*/
	}/*else the channel was previously working good with the current ip address but now fails, so let's notify the error*/
755 756
	
	obj->state=BELLE_SIP_CHANNEL_ERROR;
757
	channel_end_send_background_task(obj);
758
	/*Because error notification will in practice trigger the destruction of possible transactions and this channel,
759 760 761
	* it is safer to invoke the listener outside the current call stack.
	* Indeed the channel encounters network errors while being called for transmiting by a transaction.
	*/
762
	belle_sip_main_loop_do_later(obj->stack->ml,(belle_sip_callback_t)channel_invoke_state_listener_defered,belle_sip_object_ref(obj));
763 764
}

765 766 767 768 769 770 771 772
int belle_sip_channel_notify_timeout(belle_sip_channel_t *obj){
	const int too_long=60;
	if (belle_sip_time_ms() - obj->last_recv_time>=(too_long * 1000)){
		belle_sip_message("A timeout related to this channel occured and no message received during last %i seconds. This channel is suspect, moving to error state",too_long);
		channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
		return TRUE;
	}
	return FALSE;
Simon Morlat's avatar
Simon Morlat committed
773 774
}

775 776 777 778 779 780 781
void channel_set_state(belle_sip_channel_t *obj, belle_sip_channel_state_t state) {
	belle_sip_message("channel %p: state %s",obj,belle_sip_channel_state_to_string(state));
	
	if (state==BELLE_SIP_CHANNEL_ERROR){
		belle_sip_channel_handle_error(obj);
	}else{
		obj->state=state;
782
		if (state==BELLE_SIP_CHANNEL_DISCONNECTED){
783
			channel_end_send_background_task(obj);
784
		}
Simon Morlat's avatar
Simon Morlat committed
785
		channel_invoke_state_listener(obj);
786
	}
787
}
788

Simon Morlat's avatar
Simon Morlat committed
789
static void _send_message(belle_sip_channel_t *obj, belle_sip_message_t *msg){
790
	char buffer[belle_sip_network_buffer_size];
791
	size_t len=0;
792
	int ret=0;
Ghislain MARY's avatar
Ghislain MARY committed
793
	belle_sip_error_code error=BELLE_SIP_OK;
Simon Morlat's avatar
Simon Morlat committed
794
	
795
	BELLE_SIP_INVOKE_LISTENERS_ARG1_ARG2(obj->listeners,belle_sip_channel_listener_t,on_sending,obj,msg);
Ghislain MARY's avatar
Ghislain MARY committed
796 797
	error=belle_sip_object_marshal((belle_sip_object_t*)msg,buffer,sizeof(buffer),&len);
	if ((error==BELLE_SIP_OK) && (len>0)){
798
		buffer[len]='\0';
799 800 801 802 803 804 805 806
		if (!obj->stack->send_error)
			ret=belle_sip_channel_send(obj,buffer,len);
		else
			/*debug case*/
			ret=obj->stack->send_error;

		if (ret<0){
			belle_sip_error("channel [%p]: could not send [%i] bytes from [%s://%s:%i]  to [%s:%i]"	,obj
Simon Morlat's avatar
Simon Morlat committed
807
				,(int)len
Simon Morlat's avatar
Simon Morlat committed
808 809 810 811 812
				,belle_sip_channel_get_transport_name(obj)
				,obj->local_ip
				,obj->local_port
				,obj->peer_name
				,obj->peer_port);
813
			channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
Simon Morlat's avatar
Simon Morlat committed
814 815 816 817 818 819 820 821
		}else if (len==ret){
			belle_sip_message("channel [%p]: message sent to [%s://%s:%i], size: [%i] bytes\n%s"
								,obj
								,belle_sip_channel_get_transport_name(obj)
								,obj->peer_name
								,obj->peer_port
								,ret
								,buffer);
822
		}else{
Simon Morlat's avatar
Simon Morlat committed
823
			belle_sip_error("channel [%p]: message partly sent to [%s://%s:%i], sent: [%i/%i] bytes:\n%s"
jehan's avatar
jehan committed
824 825 826 827
								,obj
								,belle_sip_channel_get_transport_name(obj)
								,obj->peer_name
								,obj->peer_port
Simon Morlat's avatar
Simon Morlat committed
828
								,ret
Simon Morlat's avatar
Simon Morlat committed
829
								,(int)len
jehan's avatar
jehan committed
830
								,buffer);
831 832 833 834
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
835 836 837 838 839 840 841 842
/* 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){
843
	belle_sip_message("on_delayed_send_do(): sending now");
Simon Morlat's avatar
Simon Morlat committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
	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);
859
		belle_sip_message("channel %p: message sending delayed by %i ms",obj,obj->stack->tx_delay);
Simon Morlat's avatar
Simon Morlat committed
860 861
	}else _send_message(obj,msg);
}
862

Simon Morlat's avatar
Simon Morlat committed
863
void belle_sip_channel_prepare(belle_sip_channel_t *obj){
864
	channel_prepare_continue(obj);
Simon Morlat's avatar
Simon Morlat committed
865 866
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880
static void channel_push_outgoing(belle_sip_channel_t *obj, belle_sip_message_t *msg){
	obj->outgoing_messages=belle_sip_list_append(obj->outgoing_messages,msg);
}

static belle_sip_message_t *channel_pop_outgoing(belle_sip_channel_t *obj){
	belle_sip_message_t *msg=NULL;
	if (obj->outgoing_messages){
		msg=(belle_sip_message_t*)obj->outgoing_messages->data;
		obj->outgoing_messages=belle_sip_list_delete_link(obj->outgoing_messages,obj->outgoing_messages);
	}
	return msg;
}

static void channel_prepare_continue(belle_sip_channel_t *obj){
Simon Morlat's avatar
Simon Morlat committed
881 882
	switch(obj->state){
		case BELLE_SIP_CHANNEL_INIT:
883
			channel_begin_send_background_task(obj);
884
			belle_sip_channel_resolve(obj);
Simon Morlat's avatar
Simon Morlat committed
885 886
		break;
		case BELLE_SIP_CHANNEL_RES_DONE:
887
			belle_sip_channel_connect(obj);
Simon Morlat's avatar
Simon Morlat committed
888 889
		break;
		case BELLE_SIP_CHANNEL_READY:
890
			channel_process_queue(obj);
Simon Morlat's avatar
Simon Morlat committed
891 892 893
		break;
		default:
		break;
894
	}
895 896 897 898 899 900
}

static void channel_process_queue(belle_sip_channel_t *obj){
	belle_sip_message_t *msg;
	belle_sip_object_ref(obj);/* we need to ref ourself because code below may trigger our destruction*/

901
	while((msg=channel_pop_outgoing(obj))!=NULL && obj->state==BELLE_SIP_CHANNEL_READY) {
902 903 904 905
		send_message(obj, msg);
		belle_sip_object_unref(msg);
	}

Simon Morlat's avatar
Simon Morlat committed
906
	belle_sip_object_unref(obj);
907 908
}

jehan's avatar
jehan committed
909
void belle_sip_channel_set_ready(belle_sip_channel_t *obj, const struct sockaddr *addr, socklen_t slen){
910 911 912 913
	char name[NI_MAXHOST];
	char serv[NI_MAXSERV];

	if (obj->local_ip==NULL){
914 915
		struct sockaddr_storage saddr;
		socklen_t slen2=sizeof(saddr);
916
		int err;
917 918 919
		
		belle_sip_address_remove_v4_mapping(addr,(struct sockaddr*) &saddr,&slen2);
		
920
		err=getnameinfo((struct sockaddr*)&saddr,slen,name,sizeof(name),serv,sizeof(serv),NI_NUMERICHOST|NI_NUMERICSERV);
921
		if (err!=0){
922
			belle_sip_error("belle_sip_channel_set_ready(): getnameinfo() failed: %s",gai_strerror(err));
923 924 925 926 927 928 929 930
		}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);
	channel_process_queue(obj);
931
	channel_end_send_background_task(obj);
932 933
}

934
static void channel_res_done(void *data, const char *name, struct addrinfo *ai_list){
935
	belle_sip_channel_t *obj=(belle_sip_channel_t*)data;
936 937 938 939
	if (obj->resolver_ctx){
		belle_sip_object_unref(obj->resolver_ctx);
		obj->resolver_ctx=NULL;
	}
940 941
	if (ai_list){
		obj->peer_list=obj->current_peer=ai_list;
942
		channel_set_state(obj,BELLE_SIP_CHANNEL_RES_DONE);
943
		channel_prepare_continue(obj);
944
	}else{
945
		belle_sip_error("%s: DNS resolution failed", __FUNCTION__);
946
		channel_set_state(obj,BELLE_SIP_CHANNEL_ERROR);
947 948 949
	}
}

jehan's avatar
jehan committed
950
void belle_sip_channel_resolve(belle_sip_channel_t *obj){
951
	channel_set_state(obj,BELLE_SIP_CHANNEL_RES_IN_PROGRESS);
Simon Morlat's avatar
Simon Morlat committed
952 953
	if (belle_sip_stack_dns_srv_enabled(obj->stack) && obj->lp!=NULL)
		obj->resolver_ctx=belle_sip_stack_resolve(obj->stack, belle_sip_channel_get_transport_name_lower_case(obj), obj->peer_name, obj->peer_port, obj->ai_family, channel_res_done, obj);
954
	else
Simon Morlat's avatar
Simon Morlat committed
955
		obj->resolver_ctx=belle_sip_stack_resolve_a(obj->stack, obj->peer_name, obj->peer_port, obj->ai_family, channel_res_done, obj);
956 957
	if (obj->resolver_ctx){
		belle_sip_object_ref(obj->resolver_ctx);
958
	}