refresher.c 28 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2012  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
jehan's avatar
jehan committed
8 9 10 11 12 13 14 15 16 17 18 19
    (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
20
#include "belle-sip/refresher.h"
jehan's avatar
jehan committed
21

jehan's avatar
jehan committed
22
#define DEFAULT_RETRY_AFTER 60000
23
#define DEFAULT_INITIAL_RETRY_AFTER_ON_IO_ERROR 500
24

jehan's avatar
jehan committed
25 26 27 28
typedef enum belle_sip_refresher_state {
	started,
	stopped
}belle_sip_refresher_state_t;
Simon Morlat's avatar
Simon Morlat committed
29

30 31 32 33 34
typedef enum timer_purpose{
	NORMAL_REFRESH,
	RETRY
}timer_purpose_t;

jehan's avatar
jehan committed
35 36 37 38 39
struct belle_sip_refresher {
	belle_sip_object_t obj;
	belle_sip_refresher_listener_t listener;
	belle_sip_source_t* timer;
	belle_sip_client_transaction_t* transaction;
Simon Morlat's avatar
Simon Morlat committed
40 41
	int target_expires;
	int obtained_expires;
jehan's avatar
jehan committed
42
	belle_sip_refresher_state_t state;
jehan's avatar
jehan committed
43
	void* user_data;
jehan's avatar
jehan committed
44
	int retry_after;
jehan's avatar
jehan committed
45
	belle_sip_list_t* auth_events;
46
	int auth_failures;
jehan's avatar
jehan committed
47
	int on_io_error; /*flag to avoid multiple error notification*/
48
	int number_of_retry; /*counter to count number of unsuccesfull retry, used to know when to retry*/
49 50
	timer_purpose_t timer_purpose;
	unsigned char manual;
jehan's avatar
jehan committed
51
};
jehan's avatar
jehan committed
52 53 54
static int set_expires_from_trans(belle_sip_refresher_t* refresher);

static int timer_cb(void *user_data, unsigned int events) ;
55
static int belle_sip_refresher_refresh_internal(belle_sip_refresher_t* refresher,int expires,int auth_mandatory, belle_sip_list_t** auth_infos, belle_sip_uri_t *requri);
jehan's avatar
jehan committed
56

jehan's avatar
jehan committed
57

58
static void schedule_timer_at(belle_sip_refresher_t* refresher,int delay, timer_purpose_t purpose) {
Simon Morlat's avatar
Simon Morlat committed
59
	belle_sip_message("Refresher: scheduling next timer in %i ms",delay);
60
	refresher->timer_purpose=purpose;
Simon Morlat's avatar
Simon Morlat committed
61 62 63
	if (refresher->timer){
		belle_sip_main_loop_remove_source(belle_sip_stack_get_main_loop(refresher->transaction->base.provider->stack),refresher->timer);
		belle_sip_object_unref(refresher->timer);
jehan's avatar
jehan committed
64
	}
Simon Morlat's avatar
Simon Morlat committed
65 66 67
	refresher->timer=belle_sip_timeout_source_new(timer_cb,refresher,delay);
	belle_sip_object_set_name((belle_sip_object_t*)refresher->timer,"Refresher timeout");
	belle_sip_main_loop_add_source(belle_sip_stack_get_main_loop(refresher->transaction->base.provider->stack),refresher->timer);
jehan's avatar
jehan committed
68
}
69

70
static void retry_later(belle_sip_refresher_t* refresher) {
71
	refresher->number_of_retry++;
72
	schedule_timer_at(refresher,refresher->retry_after,RETRY);
jehan's avatar
jehan committed
73
}
Simon Morlat's avatar
Simon Morlat committed
74

75 76 77
static void retry_later_on_io_error(belle_sip_refresher_t* refresher) {
	/*if first retry, sent it in 500 ms*/
	if (refresher->number_of_retry < 1) {
78
		schedule_timer_at(refresher,DEFAULT_INITIAL_RETRY_AFTER_ON_IO_ERROR,RETRY);
79 80 81 82 83 84
		refresher->number_of_retry++;
	} else {
		retry_later(refresher);
	}
}

jehan's avatar
jehan committed
85
static void schedule_timer(belle_sip_refresher_t* refresher) {
86
	schedule_timer_at(refresher,refresher->obtained_expires*900,NORMAL_REFRESH);
jehan's avatar
jehan committed
87 88
}

89
static void process_dialog_terminated(belle_sip_listener_t *user_ctx, const belle_sip_dialog_terminated_event_t *event){
jehan's avatar
jehan committed
90
	/*nop*/
jehan's avatar
jehan committed
91
}
92
static void process_io_error(belle_sip_listener_t *user_ctx, const belle_sip_io_error_event_t *event){
93 94
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
	belle_sip_client_transaction_t*client_transaction;
jehan's avatar
jehan committed
95 96 97
	if (refresher->on_io_error==1) {
		return; /*refresher already on error*/
	}
Simon Morlat's avatar
Simon Morlat committed
98
	if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event),belle_sip_client_transaction_t)) {
99
		client_transaction=BELLE_SIP_CLIENT_TRANSACTION(belle_sip_io_error_event_get_source(event));
100
		if (!refresher || (refresher && ((refresher->state==stopped
Simon Morlat's avatar
Simon Morlat committed
101 102 103
			&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) != BELLE_SIP_TRANSACTION_TRYING
			&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) != BELLE_SIP_TRANSACTION_INIT /*to cover dns or certificate error*/)
			|| client_transaction !=refresher->transaction )))
104
				return; /*not for me or no longuer involved*/
105

Simon Morlat's avatar
Simon Morlat committed
106
		if (refresher->target_expires==0
107 108 109 110
				&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) != BELLE_SIP_TRANSACTION_TRYING
				&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) != BELLE_SIP_TRANSACTION_INIT ) {
			return; /*not for me or no longuer involved because expire=0*/
		}
111
		if (refresher->state==started) retry_later_on_io_error(refresher);
jehan's avatar
jehan committed
112
		if (refresher->listener) refresher->listener(refresher,refresher->user_data,503, "io error");
113

114
		return;
Simon Morlat's avatar
Simon Morlat committed
115
	} else if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event),belle_sip_provider_t)) {
116
		/*something went wrong on this provider, checking if my channel is still up*/
jehan's avatar
jehan committed
117
		if (refresher->state==started  /*refresher started or trying to refresh */
jehan's avatar
jehan committed
118
				&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) == BELLE_SIP_TRANSACTION_TERMINATED /*else we are notified by transaction error*/
119
				&& refresher->transaction->base.channel /*transaction may not have any channel*/
jehan's avatar
jehan committed
120
				&&	(belle_sip_channel_get_state(refresher->transaction->base.channel) == BELLE_SIP_CHANNEL_DISCONNECTED
121 122 123 124 125
								||belle_sip_channel_get_state(refresher->transaction->base.channel) == BELLE_SIP_CHANNEL_ERROR)) {
			belle_sip_message("refresher [%p] has channel [%p] in state [%s], reporting error"
								,refresher
								,refresher->transaction->base.channel
								,belle_sip_channel_state_to_string(belle_sip_channel_get_state(refresher->transaction->base.channel)));
126
			if (refresher->state==started) retry_later_on_io_error(refresher);
jehan's avatar
jehan committed
127
			if (refresher->listener) refresher->listener(refresher,refresher->user_data,503, "io error");
jehan's avatar
jehan committed
128
			refresher->on_io_error=1;
129 130 131
		}
		return;
	}else {
jehan's avatar
jehan committed
132 133
		/*belle_sip_error("Refresher process_io_error not implemented yet for non transaction/provider source");*/
		/*nop, because already handle at transaction layer*/
134
	}
jehan's avatar
jehan committed
135 136
}

137
belle_sip_header_contact_t* get_first_contact_in_unknown_state(belle_sip_request_t *req){
138 139 140 141 142 143
	/*check if automatic contacts could be set by provider, if not resubmit the request immediately.*/
	belle_sip_header_contact_t *contact;
	const belle_sip_list_t *l;
	for (l=belle_sip_message_get_headers((belle_sip_message_t*)req,"Contact");l!=NULL;l=l->next){
		contact=(belle_sip_header_contact_t*)l->data;
		if (belle_sip_header_contact_is_unknown(contact)){
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
			return contact;
		}
	}
	return NULL;
}

static int is_contact_address_acurate(const belle_sip_refresher_t* refresher,belle_sip_request_t* request) {
	belle_sip_header_contact_t* contact;
	if ((contact = get_first_contact_in_unknown_state(request))){
		/*check if contact ip/port is consistant with  public channel ip/port*/
		int channel_public_port = refresher->transaction->base.channel->public_port;
		int contact_port = belle_sip_uri_get_listening_port(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact)));
		const char* channel_public_ip = refresher->transaction->base.channel->public_ip;
		const char* contact_ip = belle_sip_uri_get_host(belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact)));

		if (channel_public_port == contact_port
				&& channel_public_ip && contact_ip
				&& strcmp(channel_public_ip,contact_ip) == 0) {
			/*nothing to do contact is accurate*/
			belle_sip_header_contact_set_unknown(contact,FALSE);
			return TRUE;
		} else {
			belle_sip_message("Refresher [%p]: contact address [%s:%i] does not match channel address[%s:%i]."	,refresher
					,contact_ip
					,contact_port
					,channel_public_ip
					,channel_public_port);
171 172
			return FALSE;
		}
173 174 175
	} else {
		belle_sip_message("Refresher [%p]:  has no contact for request [%p].", refresher,request);
		return TRUE;
176 177
	}
}
jehan's avatar
jehan committed
178

179
static void process_response_event(belle_sip_listener_t *user_ctx, const belle_sip_response_event_t *event){
jehan's avatar
jehan committed
180 181
	belle_sip_client_transaction_t* client_transaction = belle_sip_response_event_get_client_transaction(event);
	belle_sip_response_t* response = belle_sip_response_event_get_response(event);
jehan's avatar
jehan committed
182
	belle_sip_request_t* request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(client_transaction));
jehan's avatar
jehan committed
183 184
	int response_code = belle_sip_response_get_status_code(response);
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
185
	belle_sip_header_contact_t *contact;
jehan's avatar
jehan committed
186

jehan's avatar
jehan committed
187 188 189
	if (refresher && (client_transaction !=refresher->transaction))
		return; /*not for me*/

jehan's avatar
jehan committed
190 191
	/*handle authorization*/
	switch (response_code) {
192 193
	case 200:
		refresher->auth_failures=0;
194
		refresher->number_of_retry=0;
jehan's avatar
jehan committed
195
		/*great, success*/
jehan's avatar
jehan committed
196 197 198 199 200 201 202 203 204
		if (strcmp(belle_sip_request_get_method(request),"PUBLISH")==0) {
			/*search for etag*/
			belle_sip_header_t* etag=belle_sip_message_get_header(BELLE_SIP_MESSAGE(response),"SIP-ETag");
			if (etag) {
				belle_sip_header_t* sip_if_match = belle_sip_header_create("SIP-If-Match",belle_sip_header_extension_get_value(BELLE_SIP_HEADER_EXTENSION(etag)));
				/*update request for next refresh*/
				belle_sip_message_remove_header(BELLE_SIP_MESSAGE(request),"SIP-If-Match");
				belle_sip_message_add_header(BELLE_SIP_MESSAGE(request),sip_if_match);
			} else {
205
				belle_sip_warning("Refresher [%p] receive 200ok to a publish without etag",refresher);
jehan's avatar
jehan committed
206 207
			}
		}
jehan's avatar
jehan committed
208 209
		/*update expire if needed*/
		set_expires_from_trans(refresher);
Simon Morlat's avatar
Simon Morlat committed
210
		if (refresher->target_expires<=0) {
211 212 213
			belle_sip_refresher_stop(refresher); /*doesn't not make sense to refresh if expire =0;*/
		}
		if (refresher->state==started) {
214 215 216 217
			if (is_contact_address_acurate(refresher,request)) {
				schedule_timer(refresher); /*re-arm timer*/
			} else {
				belle_sip_message("belle_sip_refresher_start(): refresher [%p] is resubmitting request because contact sent was not correct in original request.",refresher);
218 219 220
				belle_sip_refresher_refresh(refresher,refresher->target_expires);
				return;
			}
221
		}
222
		else belle_sip_message("Refresher [%p] not scheduling next refresh, because it was stopped",refresher);
jehan's avatar
jehan committed
223
		break;
224 225 226 227
	case 301:
	case 302:
		contact=belle_sip_message_get_header_by_type(response,belle_sip_header_contact_t);
		if (contact){
Simon Morlat's avatar
Simon Morlat committed
228 229
			belle_sip_uri_t *uri=belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact));
			if (uri && belle_sip_refresher_refresh_internal(refresher,refresher->target_expires,TRUE,&refresher->auth_events,uri)==0)
230 231 232
				return;
		}
		break;
jehan's avatar
jehan committed
233
	case 401:
234 235
	case 407:
		refresher->auth_failures++;
236
		if (refresher->auth_failures>1){
237
			/*avoid looping with 407 or 401 */
238
			belle_sip_warning("Authentication is failing constantly, %s",(refresher->target_expires>0)? "will retry later":"giving up.");
Simon Morlat's avatar
Simon Morlat committed
239
			if (refresher->target_expires>0) retry_later(refresher);
240 241
			break;
		}
jehan's avatar
jehan committed
242 243
		if (refresher->auth_events) {
			refresher->auth_events=belle_sip_list_free_with_data(refresher->auth_events,(void (*)(void*))belle_sip_auth_event_destroy);
jehan's avatar
jehan committed
244
		}
245
		if (belle_sip_refresher_refresh_internal(refresher,refresher->target_expires,TRUE,&refresher->auth_events,NULL)==0)
jehan's avatar
jehan committed
246
			return; /*ok, keep 401 internal*/
247
		break; /*Else notify user of registration failure*/
248 249 250 251
	case 403:
		/*In case of 403, we will retry later, just in case*/
		if (refresher->target_expires>0) retry_later(refresher);
		break;
252 253 254 255 256 257
	case 423:{
		belle_sip_header_extension_t *min_expires=BELLE_SIP_HEADER_EXTENSION(belle_sip_message_get_header((belle_sip_message_t*)response,"Min-Expires"));
		if (min_expires){
			const char *value=belle_sip_header_extension_get_value(min_expires);
			if (value){
				int new_expires=atoi(value);
Simon Morlat's avatar
Simon Morlat committed
258
				if (new_expires>0 && refresher->state==started){
Simon Morlat's avatar
Simon Morlat committed
259 260
					refresher->target_expires=new_expires;
					belle_sip_refresher_refresh(refresher,refresher->target_expires);
261 262 263 264 265
					return;
				}
			}
		}else belle_sip_warning("Receiving 423 but no min-expires header.");
		break;
jehan's avatar
jehan committed
266 267 268 269 270
	}
	case 408:
	case 480:
	case 503:
	case 504:
Simon Morlat's avatar
Simon Morlat committed
271
		if (refresher->target_expires>0) retry_later(refresher);
jehan's avatar
jehan committed
272 273 274 275 276
		break;
	default:
		break;
	}
	if (refresher->listener) refresher->listener(refresher,refresher->user_data,response_code, belle_sip_response_get_reason_phrase(response));
jehan's avatar
jehan committed
277 278

}
279 280

static void process_timeout(belle_sip_listener_t *user_ctx, const belle_sip_timeout_event_t *event) {
jehan's avatar
jehan committed
281
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
jehan's avatar
jehan committed
282 283
	belle_sip_client_transaction_t*client_transaction =belle_sip_timeout_event_get_client_transaction(event);

jehan's avatar
jehan committed
284
	if (refresher && (client_transaction !=refresher->transaction))
Simon Morlat's avatar
Simon Morlat committed
285
		return; /*not for me*/
jehan's avatar
jehan committed
286

Simon Morlat's avatar
Simon Morlat committed
287
	if (refresher->state==started) {
Simon Morlat's avatar
Simon Morlat committed
288
		/*retry in 2 seconds but not immediately to let the current transaction be cleaned*/
289
		schedule_timer_at(refresher,2000,RETRY);
Simon Morlat's avatar
Simon Morlat committed
290
	}
jehan's avatar
jehan committed
291
	if (refresher->listener) refresher->listener(refresher,refresher->user_data,408, "timeout");
jehan's avatar
jehan committed
292
}
Simon Morlat's avatar
Simon Morlat committed
293

294
static void process_transaction_terminated(belle_sip_listener_t *user_ctx, const belle_sip_transaction_terminated_event_t *event) {
jehan's avatar
jehan committed
295
	/*belle_sip_message("process_transaction_terminated Transaction terminated [%p]",event);*/
jehan's avatar
jehan committed
296 297
}

298 299 300 301 302 303
static void process_auth_requested(belle_sip_listener_t *l, belle_sip_auth_event_t *event){
}

static void process_request_event(belle_sip_listener_t *user_ctx, const belle_sip_request_event_t *event){
}

jehan's avatar
jehan committed
304
static void destroy(belle_sip_refresher_t *refresher){
305
	belle_sip_refresher_stop(refresher);
306
	belle_sip_provider_remove_internal_sip_listener(refresher->transaction->base.provider,BELLE_SIP_LISTENER(refresher));
307
	belle_sip_object_unref(refresher->transaction);
308
	refresher->transaction=NULL;
jehan's avatar
jehan committed
309
	if (refresher->auth_events) refresher->auth_events=belle_sip_list_free_with_data(refresher->auth_events,(void (*)(void*))belle_sip_auth_event_destroy);
jehan's avatar
jehan committed
310
}
311

312 313 314 315 316 317 318 319 320 321 322
BELLE_SIP_IMPLEMENT_INTERFACE_BEGIN(belle_sip_refresher_t,belle_sip_listener_t)
	process_dialog_terminated,
	process_io_error,
	process_request_event,
	process_response_event,
	process_timeout,
	process_transaction_terminated,
	process_auth_requested
BELLE_SIP_IMPLEMENT_INTERFACE_END

BELLE_SIP_DECLARE_IMPLEMENTED_INTERFACES_1(belle_sip_refresher_t, belle_sip_listener_t);
jehan's avatar
jehan committed
323 324 325 326 327 328 329

BELLE_SIP_INSTANCIATE_VPTR(belle_sip_refresher_t, belle_sip_object_t,destroy, NULL, NULL,FALSE);

void belle_sip_refresher_set_listener(belle_sip_refresher_t* refresher, belle_sip_refresher_listener_t listener,void* user_pointer) {
	refresher->listener=listener;
	refresher->user_data=user_pointer;
}
Simon Morlat's avatar
Simon Morlat committed
330

331
int belle_sip_refresher_refresh(belle_sip_refresher_t* refresher,int expires) {
332
	return belle_sip_refresher_refresh_internal(refresher,expires,FALSE,NULL,NULL);
333
}
334

335 336 337 338
static int unfilled_auth_info(const void* info,const void* userptr) {
	belle_sip_auth_event_t* auth_info = (belle_sip_auth_event_t*)info;
	return auth_info->passwd || auth_info->ha1;
}
Simon Morlat's avatar
Simon Morlat committed
339

340
static int belle_sip_refresher_refresh_internal(belle_sip_refresher_t* refresher, int expires, int auth_mandatory, belle_sip_list_t** auth_infos, belle_sip_uri_t *requri) {
jehan's avatar
jehan committed
341
	belle_sip_request_t*old_request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
342
	belle_sip_response_t*old_response=belle_sip_transaction_get_response(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
343 344 345
	belle_sip_dialog_t* dialog = belle_sip_transaction_get_dialog(BELLE_SIP_TRANSACTION(refresher->transaction));
	belle_sip_client_transaction_t* client_transaction;
	belle_sip_request_t* request;
jehan's avatar
jehan committed
346
	belle_sip_header_expires_t* expires_header;
347
	belle_sip_uri_t* preset_route=refresher->transaction->preset_route;
jehan's avatar
jehan committed
348
	belle_sip_provider_t* prov=refresher->transaction->base.provider;
349
	belle_sip_header_contact_t* contact;
Simon Morlat's avatar
Simon Morlat committed
350
	
351
	/*first remove timer if any*/
jehan's avatar
jehan committed
352
	if (expires >=0) {
Simon Morlat's avatar
Simon Morlat committed
353
		refresher->target_expires=expires;
jehan's avatar
jehan committed
354 355 356
	} else {
		/*-1 keep last value*/
	}
jehan's avatar
jehan committed
357

jehan's avatar
jehan committed
358
	if (!dialog) {
jehan's avatar
jehan committed
359
		const belle_sip_transaction_state_t state=belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
360
		/*create new request*/
jehan's avatar
jehan committed
361 362 363
		if (belle_sip_transaction_state_is_transient(state)) {
			/*operation pending, cannot update authorization headers*/
			belle_sip_header_cseq_t* cseq;
jehan's avatar
jehan committed
364
			belle_sip_message("Refresher [%p] already have transaction [%p] in state [%s]"	,refresher
Simon Morlat's avatar
Simon Morlat committed
365 366
				,refresher->transaction
				,belle_sip_transaction_state_to_string(state));
367
			request=belle_sip_request_clone_with_body(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction)));
jehan's avatar
jehan committed
368 369 370
			cseq=belle_sip_message_get_header_by_type(request,belle_sip_header_cseq_t);
			belle_sip_header_cseq_set_seq_number(cseq,belle_sip_header_cseq_get_seq_number(cseq)+1);
		} else {
371
			request=belle_sip_client_transaction_create_authenticated_request(refresher->transaction,auth_infos);
jehan's avatar
jehan committed
372
		}
373 374 375 376 377 378 379
		if (requri){
			/*case where we are redirected*/
			belle_sip_request_set_uri(request,requri);
			/*remove auth headers, they are not valid for new destination*/
			belle_sip_message_remove_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_AUTHORIZATION);
			belle_sip_message_remove_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_PROXY_AUTHORIZATION);
		}
jehan's avatar
jehan committed
380
	} else if (dialog && belle_sip_dialog_get_state(dialog)==BELLE_SIP_DIALOG_CONFIRMED) {
Simon Morlat's avatar
Simon Morlat committed
381 382 383 384
		if (belle_sip_dialog_request_pending(dialog)){
			belle_sip_message("Cannot refresh now, there is a pending request in the dialog.");
			return -1;
		}
jehan's avatar
jehan committed
385
		request=belle_sip_dialog_create_request_from(dialog,old_request);
jehan's avatar
jehan committed
386 387
		if (strcmp(belle_sip_request_get_method(request),"SUBSCRIBE")==0) {
			/*put expire header*/
jehan's avatar
jehan committed
388 389 390 391
			if (!(expires_header = belle_sip_message_get_header_by_type(request,belle_sip_header_expires_t))) {
				expires_header = belle_sip_header_expires_new();
				belle_sip_message_add_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_HEADER(expires_header));
			}
jehan's avatar
jehan committed
392
		}
393
		belle_sip_provider_add_authorization(prov,request,old_response,NULL,auth_infos);
jehan's avatar
jehan committed
394 395 396 397 398 399 400
	} else {
		belle_sip_error("Unexpected dialog state [%s] for dialog [%p], cannot refresh [%s]"
				,belle_sip_dialog_state_to_string(belle_sip_dialog_get_state(dialog))
				,dialog
				,belle_sip_request_get_method(old_request));
		return -1;
	}
401

402
	if (auth_mandatory && auth_infos && belle_sip_list_find_custom(*auth_infos, unfilled_auth_info, NULL)) {
403 404 405 406
		belle_sip_message("Auth info not found for this refresh operation on [%p]",refresher);
		if (request) belle_sip_object_unref(request);
		return -1;
	}
407 408
	
	refresher->on_io_error=0; /*reset this flag*/
409

410 411 412
	/*update expires in any cases*/
	expires_header = belle_sip_message_get_header_by_type(request,belle_sip_header_expires_t);
	if (expires_header)
Simon Morlat's avatar
Simon Morlat committed
413
		belle_sip_header_expires_set_expires(expires_header,refresher->target_expires);
414
	contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t);
jehan's avatar
jehan committed
415
	if (contact && belle_sip_header_contact_get_expires(contact)>=0)
Simon Morlat's avatar
Simon Morlat committed
416
		belle_sip_header_contact_set_expires(contact,refresher->target_expires);
417

418 419 420 421 422 423 424 425 426
	/*update the Date header if it exists*/
	{
		belle_sip_header_date_t *date=belle_sip_message_get_header_by_type(request,belle_sip_header_date_t);
		if (date){
			time_t curtime=time(NULL);
			belle_sip_header_date_set_time(date,&curtime);
		}
	}
	
jehan's avatar
jehan committed
427 428 429 430 431 432
	client_transaction = belle_sip_provider_create_client_transaction(prov,request);
	client_transaction->base.is_internal=1;
	belle_sip_transaction_set_application_data(BELLE_SIP_TRANSACTION(client_transaction),refresher);
	/*update reference transaction for next refresh*/
	belle_sip_object_unref(refresher->transaction);
	refresher->transaction=client_transaction;
jehan's avatar
jehan committed
433
	belle_sip_object_ref(refresher->transaction);
jehan's avatar
jehan committed
434

435
	if (belle_sip_client_transaction_send_request_to(client_transaction,preset_route)) {
jehan's avatar
jehan committed
436
		belle_sip_error("Cannot send refresh method [%s] for refresher [%p]"
437
				,belle_sip_request_get_method(request)
jehan's avatar
jehan committed
438
				,refresher);
Simon Morlat's avatar
Simon Morlat committed
439
		return -1;
jehan's avatar
jehan committed
440
	}
441
	if (expires==0) belle_sip_refresher_stop(refresher);
jehan's avatar
jehan committed
442 443 444
	return 0;
}

445

jehan's avatar
jehan committed
446 447
static int timer_cb(void *user_data, unsigned int events) {
	belle_sip_refresher_t* refresher = (belle_sip_refresher_t*)user_data;
448
	
449
	refresher->auth_failures=0;/*reset the auth_failures to get a chance to authenticate again*/
450 451 452 453 454 455 456 457
	
	if (refresher->timer_purpose==NORMAL_REFRESH && refresher->manual) {
		belle_sip_message("Refresher [%p] is in manual mode, skipping refresh.",refresher);
		/*call listener with special code 0 to indicate request is about to expire*/
		if (refresher->listener) refresher->listener(refresher,refresher->user_data,0, "about to expire");
		return BELLE_SIP_STOP;
	}
	
Simon Morlat's avatar
Simon Morlat committed
458 459 460
	if (belle_sip_refresher_refresh(refresher,refresher->target_expires)==-1){
		retry_later(refresher);
	}
461
	return BELLE_SIP_STOP;
jehan's avatar
jehan committed
462
}
463

jehan's avatar
jehan committed
464 465 466 467
static belle_sip_header_contact_t* get_matching_contact(const belle_sip_transaction_t* transaction) {
	belle_sip_request_t*request=belle_sip_transaction_get_request(transaction);
	belle_sip_response_t*response=transaction->last_response;
	const belle_sip_list_t* contact_header_list;
jehan's avatar
jehan committed
468 469 470 471
	belle_sip_header_contact_t* unfixed_local_contact;
	belle_sip_header_contact_t* fixed_local_contact;
	char* tmp_string;
	char* tmp_string2;
472 473
	if (!response)
		return NULL;
jehan's avatar
jehan committed
474
	/*we assume, there is only one contact in request*/
jehan's avatar
jehan committed
475 476
	unfixed_local_contact= belle_sip_message_get_header_by_type(BELLE_SIP_MESSAGE(request),belle_sip_header_contact_t);
	fixed_local_contact= BELLE_SIP_HEADER_CONTACT(belle_sip_object_clone(BELLE_SIP_OBJECT(unfixed_local_contact)));
jehan's avatar
jehan committed
477 478

	/*first fix contact using received/rport*/
jehan's avatar
jehan committed
479
	belle_sip_response_fix_contact(response,fixed_local_contact);
jehan's avatar
jehan committed
480
	contact_header_list = belle_sip_message_get_headers(BELLE_SIP_MESSAGE(response),BELLE_SIP_CONTACT);
jehan's avatar
jehan committed
481

jehan's avatar
jehan committed
482
	if (contact_header_list) {
Simon Morlat's avatar
Simon Morlat committed
483 484 485 486 487 488
		contact_header_list = belle_sip_list_find_custom((belle_sip_list_t*)contact_header_list
					,(belle_sip_compare_func)belle_sip_header_contact_not_equals
					, (const void*)fixed_local_contact);
		if (!contact_header_list) {
			/*reset header list*/
			contact_header_list = belle_sip_message_get_headers(BELLE_SIP_MESSAGE(response),BELLE_SIP_CONTACT);
jehan's avatar
jehan committed
489
			contact_header_list = belle_sip_list_find_custom((belle_sip_list_t*)contact_header_list
Simon Morlat's avatar
Simon Morlat committed
490 491
							,(belle_sip_compare_func)belle_sip_header_contact_not_equals
							,unfixed_local_contact);
Simon Morlat's avatar
Simon Morlat committed
492 493 494 495 496 497 498 499
		}
		if (!contact_header_list) {
			tmp_string=belle_sip_object_to_string(BELLE_SIP_OBJECT(fixed_local_contact));
			tmp_string2=belle_sip_object_to_string(BELLE_SIP_OBJECT(unfixed_local_contact));
			belle_sip_message("No matching contact neither for [%s] nor [%s]", tmp_string, tmp_string2);
			belle_sip_free(tmp_string);
			belle_sip_free(tmp_string2);
			return NULL;
jehan's avatar
jehan committed
500
		} else {
Simon Morlat's avatar
Simon Morlat committed
501 502 503
			return BELLE_SIP_HEADER_CONTACT(contact_header_list->data);
		}
	} else {
jehan's avatar
jehan committed
504 505 506 507 508 509 510 511
		return NULL;
	}

}
static int set_expires_from_trans(belle_sip_refresher_t* refresher) {
	belle_sip_transaction_t* transaction = BELLE_SIP_TRANSACTION(refresher->transaction);
	belle_sip_response_t*response=transaction->last_response;
	belle_sip_request_t*request=belle_sip_transaction_get_request(transaction);
jehan's avatar
jehan committed
512
	belle_sip_header_expires_t*  expires_header=belle_sip_message_get_header_by_type(request,belle_sip_header_expires_t);
jehan's avatar
jehan committed
513
	belle_sip_header_contact_t* contact_header;
514

Simon Morlat's avatar
Simon Morlat committed
515
	refresher->obtained_expires=-1;
516
	
jehan's avatar
jehan committed
517
	if (strcmp("REGISTER",belle_sip_request_get_method(request))==0
jehan's avatar
jehan committed
518
			|| expires_header /*if request has an expire header, refresher can always work*/) {
jehan's avatar
jehan committed
519

Simon Morlat's avatar
Simon Morlat committed
520 521 522
		if (expires_header)
			refresher->target_expires = belle_sip_header_expires_get_expires(expires_header);
		
jehan's avatar
jehan committed
523 524 525 526
		/*An "expires" parameter on the "Contact" header has no semantics for
		*   SUBSCRIBE and is explicitly not equivalent to an "Expires" header in
		*  a SUBSCRIBE request or response.
		*/
Simon Morlat's avatar
Simon Morlat committed
527 528 529 530 531 532 533 534 535 536
		if (strcmp("REGISTER",belle_sip_request_get_method(request))==0){
			if (!expires_header && (contact_header=belle_sip_message_get_header_by_type((belle_sip_message_t*)request,belle_sip_header_contact_t))){
				int ct_expires=belle_sip_header_contact_get_expires(BELLE_SIP_HEADER_CONTACT(contact_header));
				if (ct_expires!=-1) refresher->target_expires=ct_expires;
			}
			/*check in response also to get the obtained expires*/
			if ((contact_header=get_matching_contact(transaction))!=NULL){
				/*matching contact, check for its possible expires param*/
				refresher->obtained_expires=belle_sip_header_contact_get_expires(BELLE_SIP_HEADER_CONTACT(contact_header));
			}
537
		}
Simon Morlat's avatar
Simon Morlat committed
538
		if (refresher->obtained_expires==-1){
jehan's avatar
jehan committed
539
			/*no contact with expire or not relevant, looking for Expires header*/
540
			if (response && (expires_header=(belle_sip_header_expires_t*)belle_sip_message_get_header(BELLE_SIP_MESSAGE(response),BELLE_SIP_EXPIRES))) {
Simon Morlat's avatar
Simon Morlat committed
541
				refresher->obtained_expires = belle_sip_header_expires_get_expires(expires_header);
jehan's avatar
jehan committed
542 543
			}
		}
544
		if (refresher->obtained_expires==-1) {
545
			belle_sip_message("Neither Expires header nor corresponding Contact header found, checking from original request");
Simon Morlat's avatar
Simon Morlat committed
546
			refresher->obtained_expires=refresher->target_expires;
547 548 549 550 551
		}else if (refresher->target_expires>0 && refresher->obtained_expires==0){
			/*check this case because otherwise we are going to loop fast in sending refresh requests.*/
			belle_sip_warning("Server replied with 0 expires, what does this mean ?");
			/*suppose it's a server bug and assume our target_expires is understood.*/
			refresher->obtained_expires=refresher->target_expires;
jehan's avatar
jehan committed
552
		}
Simon Morlat's avatar
Simon Morlat committed
553 554
	} else if (strcmp("INVITE",belle_sip_request_get_method(request))==0) {
		belle_sip_error("Refresher does not support INVITE yet");
555
		return -1;
jehan's avatar
jehan committed
556 557
	} else {
		belle_sip_error("Refresher does not support [%s] yet",belle_sip_request_get_method(request));
558
		return -1;
jehan's avatar
jehan committed
559 560 561 562 563 564
	}
	return 0;
}


int belle_sip_refresher_start(belle_sip_refresher_t* refresher) {
jehan's avatar
jehan committed
565
	if(refresher->state==started) {
Simon Morlat's avatar
Simon Morlat committed
566
		belle_sip_warning("Refresher [%p] already started",refresher);
jehan's avatar
jehan committed
567
	} else {
Simon Morlat's avatar
Simon Morlat committed
568
		if (refresher->target_expires>0) {
569
			belle_sip_request_t* request = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction));
Simon Morlat's avatar
Simon Morlat committed
570
			refresher->state=started;
571
			if (is_contact_address_acurate(refresher,request)) {
572 573 574 575 576 577
				schedule_timer(refresher); /*re-arm timer*/
			} else {
				belle_sip_message("belle_sip_refresher_start(): refresher [%p] is resubmitting request because contact sent was not correct in original request.",refresher);
				belle_sip_refresher_refresh(refresher,refresher->target_expires);
				return 0;
			}
Simon Morlat's avatar
Simon Morlat committed
578
			belle_sip_message("Refresher [%p] started, next refresh in [%i] s",refresher,refresher->obtained_expires);
Simon Morlat's avatar
Simon Morlat committed
579
		}else{
Simon Morlat's avatar
Simon Morlat committed
580
			belle_sip_message("Refresher [%p] stopped, expires=%i",refresher,refresher->target_expires);
jehan's avatar
jehan committed
581
			refresher->state=stopped;
Simon Morlat's avatar
Simon Morlat committed
582
		}
jehan's avatar
jehan committed
583 584 585 586 587
	}
	return 0;
}

void belle_sip_refresher_stop(belle_sip_refresher_t* refresher) {
Simon Morlat's avatar
Simon Morlat committed
588
	belle_sip_message("Refresher [%p] stopped.",refresher);
589 590 591 592 593
	if (refresher->timer){
		belle_sip_main_loop_remove_source(belle_sip_stack_get_main_loop(refresher->transaction->base.provider->stack), refresher->timer);
		belle_sip_object_unref(refresher->timer);
		refresher->timer=NULL;
	}
jehan's avatar
jehan committed
594
	refresher->state=stopped;
jehan's avatar
jehan committed
595
}
Simon Morlat's avatar
Simon Morlat committed
596

jehan's avatar
jehan committed
597
belle_sip_refresher_t* belle_sip_refresher_new(belle_sip_client_transaction_t* transaction) {
598
	belle_sip_refresher_t* refresher;
599
	belle_sip_transaction_state_t state=belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(transaction));
jehan's avatar
jehan committed
600
	belle_sip_request_t* request = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(transaction));
601 602
	int is_register=strcmp("REGISTER",belle_sip_request_get_method(request))==0;
	
603
	refresher = (belle_sip_refresher_t*)belle_sip_object_new(belle_sip_refresher_t);
jehan's avatar
jehan committed
604
	refresher->transaction=transaction;
jehan's avatar
jehan committed
605
	refresher->state=stopped;
606
	refresher->number_of_retry=0;
jehan's avatar
jehan committed
607
	belle_sip_object_ref(transaction);
jehan's avatar
jehan committed
608
	refresher->retry_after=DEFAULT_RETRY_AFTER;
609 610
	
	belle_sip_provider_add_internal_sip_listener(transaction->base.provider,BELLE_SIP_LISTENER(refresher), is_register);
Simon Morlat's avatar
Simon Morlat committed
611
	if (set_expires_from_trans(refresher)==-1){
jehan's avatar
jehan committed
612 613
		belle_sip_error("Unable to extract refresh value from transaction [%p]",transaction);
	}
614
	if (belle_sip_transaction_state_is_transient(state)) {
615
		belle_sip_message("Refresher [%p] takes ownership of transaction [%p]",refresher,transaction);
616
		transaction->base.is_internal=1;
Simon Morlat's avatar
Simon Morlat committed
617
		refresher->state=started;
618 619
	}else{
		belle_sip_refresher_start(refresher);
620
	}
jehan's avatar
jehan committed
621 622
	return refresher;
}
Simon Morlat's avatar
Simon Morlat committed
623

624
int belle_sip_refresher_get_expires(const belle_sip_refresher_t* refresher) {
Simon Morlat's avatar
Simon Morlat committed
625
	return refresher->target_expires;
626
}
jehan's avatar
jehan committed
627 628 629 630 631 632 633 634

int belle_sip_refresher_get_retry_after(const belle_sip_refresher_t* refresher){
	return refresher->retry_after;
}

void belle_sip_refresher_set_retry_after(belle_sip_refresher_t* refresher, int delay_ms) {
	refresher->retry_after=delay_ms;
}
jehan's avatar
jehan committed
635 636 637 638 639 640
const belle_sip_client_transaction_t* belle_sip_refresher_get_transaction(const belle_sip_refresher_t* refresher) {
	return refresher->transaction;
}
const belle_sip_list_t* belle_sip_refresher_get_auth_events(const belle_sip_refresher_t* refresher) {
	return refresher->auth_events;
}
641

642 643 644 645
void belle_sip_refresher_enable_manual_mode(belle_sip_refresher_t *refresher, int enabled){
	refresher->manual=enabled;
}