refresher.c 30.2 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

25 26
static void belle_sip_refresher_stop_internal(belle_sip_refresher_t* refresher,int cancel_pending_transaction) ;

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

32 33 34 35 36
typedef enum timer_purpose{
	NORMAL_REFRESH,
	RETRY
}timer_purpose_t;

jehan's avatar
jehan committed
37 38 39 40 41
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;
42
	char* realm;
Simon Morlat's avatar
Simon Morlat committed
43 44
	int target_expires;
	int obtained_expires;
jehan's avatar
jehan committed
45
	belle_sip_refresher_state_t state;
jehan's avatar
jehan committed
46
	void* user_data;
jehan's avatar
jehan committed
47
	int retry_after;
jehan's avatar
jehan committed
48
	belle_sip_list_t* auth_events;
49
	int auth_failures;
jehan's avatar
jehan committed
50
	int on_io_error; /*flag to avoid multiple error notification*/
51
	int number_of_retry; /*counter to count number of unsuccesfull retry, used to know when to retry*/
52 53
	timer_purpose_t timer_purpose;
	unsigned char manual;
jehan's avatar
jehan committed
54
};
jehan's avatar
jehan committed
55 56 57
static int set_expires_from_trans(belle_sip_refresher_t* refresher);

static int timer_cb(void *user_data, unsigned int events) ;
58
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
59

60
static void cancel_retry(belle_sip_refresher_t* refresher) {
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);
64
		refresher->timer=NULL;
jehan's avatar
jehan committed
65
	}
66 67 68 69 70 71
}
static void schedule_timer_at(belle_sip_refresher_t* refresher,int delay, timer_purpose_t purpose) {
	belle_sip_message("Refresher: scheduling next timer in %i ms",delay);
	refresher->timer_purpose=purpose;
	/*cancel timer if any*/
	cancel_retry(refresher);
Simon Morlat's avatar
Simon Morlat committed
72 73 74
	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
75
}
76

77
static void retry_later(belle_sip_refresher_t* refresher) {
78
	refresher->number_of_retry++;
79
	schedule_timer_at(refresher,refresher->retry_after,RETRY);
jehan's avatar
jehan committed
80
}
Simon Morlat's avatar
Simon Morlat committed
81

82 83 84
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) {
85
		schedule_timer_at(refresher,DEFAULT_INITIAL_RETRY_AFTER_ON_IO_ERROR,RETRY);
86 87 88 89 90 91
		refresher->number_of_retry++;
	} else {
		retry_later(refresher);
	}
}

jehan's avatar
jehan committed
92
static void schedule_timer(belle_sip_refresher_t* refresher) {
93
	schedule_timer_at(refresher,refresher->obtained_expires*900,NORMAL_REFRESH);
jehan's avatar
jehan committed
94 95
}

96
static void process_dialog_terminated(belle_sip_listener_t *user_ctx, const belle_sip_dialog_terminated_event_t *event){
jehan's avatar
jehan committed
97
	/*nop*/
jehan's avatar
jehan committed
98
}
99
static void process_io_error(belle_sip_listener_t *user_ctx, const belle_sip_io_error_event_t *event){
100 101
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
	belle_sip_client_transaction_t*client_transaction;
jehan's avatar
jehan committed
102 103 104
	if (refresher->on_io_error==1) {
		return; /*refresher already on error*/
	}
Simon Morlat's avatar
Simon Morlat committed
105
	if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event),belle_sip_client_transaction_t)) {
106
		client_transaction=BELLE_SIP_CLIENT_TRANSACTION(belle_sip_io_error_event_get_source(event));
107
		if (!refresher || (refresher && ((refresher->state==stopped
Simon Morlat's avatar
Simon Morlat committed
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 /*to cover dns or certificate error*/)
			|| client_transaction !=refresher->transaction )))
111
				return; /*not for me or no longuer involved*/
112

Simon Morlat's avatar
Simon Morlat committed
113
		if (refresher->target_expires==0
114 115 116 117
				&& 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*/
		}
118
		if (refresher->state==started) retry_later_on_io_error(refresher);
jehan's avatar
jehan committed
119
		if (refresher->listener) refresher->listener(refresher,refresher->user_data,503, "io error");
120

121
		return;
Simon Morlat's avatar
Simon Morlat committed
122
	} else if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event),belle_sip_provider_t)) {
123
		/*something went wrong on this provider, checking if my channel is still up*/
jehan's avatar
jehan committed
124
		if (refresher->state==started  /*refresher started or trying to refresh */
jehan's avatar
jehan committed
125
				&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) == BELLE_SIP_TRANSACTION_TERMINATED /*else we are notified by transaction error*/
126
				&& refresher->transaction->base.channel /*transaction may not have any channel*/
jehan's avatar
jehan committed
127
				&&	(belle_sip_channel_get_state(refresher->transaction->base.channel) == BELLE_SIP_CHANNEL_DISCONNECTED
128 129 130 131 132
								||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)));
133
			if (refresher->state==started) retry_later_on_io_error(refresher);
jehan's avatar
jehan committed
134
			if (refresher->listener) refresher->listener(refresher,refresher->user_data,503, "io error");
jehan's avatar
jehan committed
135
			refresher->on_io_error=1;
136 137 138
		}
		return;
	}else {
jehan's avatar
jehan committed
139 140
		/*belle_sip_error("Refresher process_io_error not implemented yet for non transaction/provider source");*/
		/*nop, because already handle at transaction layer*/
141
	}
jehan's avatar
jehan committed
142 143
}

144
belle_sip_header_contact_t* get_first_contact_in_unknown_state(belle_sip_request_t *req){
145 146 147 148 149 150
	/*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)){
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
			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);
178 179
			return FALSE;
		}
180 181 182
	} else {
		belle_sip_message("Refresher [%p]:  has no contact for request [%p].", refresher,request);
		return TRUE;
183 184
	}
}
jehan's avatar
jehan committed
185

186
static void process_response_event(belle_sip_listener_t *user_ctx, const belle_sip_response_event_t *event){
jehan's avatar
jehan committed
187 188
	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
189
	belle_sip_request_t* request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(client_transaction));
jehan's avatar
jehan committed
190 191
	int response_code = belle_sip_response_get_status_code(response);
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
192
	belle_sip_header_contact_t *contact;
jehan's avatar
jehan committed
193

jehan's avatar
jehan committed
194 195 196
	if (refresher && (client_transaction !=refresher->transaction))
		return; /*not for me*/

197 198
	/*success case:*/
	if (response_code>=200 && response_code<300){
199
		refresher->auth_failures=0;
200
		refresher->number_of_retry=0;
jehan's avatar
jehan committed
201
		/*great, success*/
jehan's avatar
jehan committed
202 203 204 205 206 207 208 209 210
		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 {
211
				belle_sip_warning("Refresher [%p] receive 200ok to a publish without etag",refresher);
jehan's avatar
jehan committed
212 213
			}
		}
jehan's avatar
jehan committed
214 215
		/*update expire if needed*/
		set_expires_from_trans(refresher);
Simon Morlat's avatar
Simon Morlat committed
216
		if (refresher->target_expires<=0) {
217 218 219
			belle_sip_refresher_stop(refresher); /*doesn't not make sense to refresh if expire =0;*/
		}
		if (refresher->state==started) {
220 221 222 223
			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);
224 225 226
				belle_sip_refresher_refresh(refresher,refresher->target_expires);
				return;
			}
227
		}
228
		else belle_sip_message("Refresher [%p] not scheduling next refresh, because it was stopped",refresher);
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 255 256 257
	}else{/*special error cases*/
		switch (response_code) {
		case 301:
		case 302:
			contact=belle_sip_message_get_header_by_type(response,belle_sip_header_contact_t);
			if (contact){
				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)
					return;
			}
			break;
		case 401:
		case 407:
			refresher->auth_failures++;
			if (refresher->auth_failures>1){
				/*avoid looping with 407 or 401 */
				belle_sip_warning("Authentication is failing constantly, %s",(refresher->target_expires>0)? "will retry later":"giving up.");
				if (refresher->target_expires>0) retry_later(refresher);
				refresher->auth_failures=0; /*reset auth failure*/
				break;
			}
			if (refresher->auth_events) {
				refresher->auth_events=belle_sip_list_free_with_data(refresher->auth_events,(void (*)(void*))belle_sip_auth_event_destroy);
			}
			if (belle_sip_refresher_refresh_internal(refresher,refresher->target_expires,TRUE,&refresher->auth_events,NULL)==0)
				return; /*ok, keep 401 internal*/
			break; /*Else notify user of registration failure*/
		case 403:
			/*In case of 403, we will retry later, just in case*/
Simon Morlat's avatar
Simon Morlat committed
258
			if (refresher->target_expires>0) retry_later(refresher);
259 260 261 262 263 264 265 266 267 268 269 270 271 272
			break;
		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);
					if (new_expires>0 && refresher->state==started){
						refresher->target_expires=new_expires;
						belle_sip_refresher_refresh(refresher,refresher->target_expires);
						return;
					}
				}
			}else belle_sip_warning("Receiving 423 but no min-expires header.");
273 274
			break;
		}
275 276 277
		case 505:
		case 501:
			/*irrecoverable errors, probably no need to retry later*/
278 279
			break;
		default:
280 281
			/*for all other errors, retry later*/
			if (refresher->target_expires>0) retry_later(refresher);
282
			break;
jehan's avatar
jehan committed
283
		}
jehan's avatar
jehan committed
284 285
	}
	if (refresher->listener) refresher->listener(refresher,refresher->user_data,response_code, belle_sip_response_get_reason_phrase(response));
jehan's avatar
jehan committed
286 287

}
288 289

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

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

Simon Morlat's avatar
Simon Morlat committed
296
	if (refresher->state==started) {
Simon Morlat's avatar
Simon Morlat committed
297
		/*retry in 2 seconds but not immediately to let the current transaction be cleaned*/
298
		schedule_timer_at(refresher,2000,RETRY);
Simon Morlat's avatar
Simon Morlat committed
299
	}
jehan's avatar
jehan committed
300
	if (refresher->listener) refresher->listener(refresher,refresher->user_data,408, "timeout");
jehan's avatar
jehan committed
301
}
Simon Morlat's avatar
Simon Morlat committed
302

303
static void process_transaction_terminated(belle_sip_listener_t *user_ctx, const belle_sip_transaction_terminated_event_t *event) {
jehan's avatar
jehan committed
304
	/*belle_sip_message("process_transaction_terminated Transaction terminated [%p]",event);*/
jehan's avatar
jehan committed
305 306
}

307 308 309 310 311 312
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
313
static void destroy(belle_sip_refresher_t *refresher){
314
	belle_sip_refresher_stop(refresher);
315
	belle_sip_provider_remove_internal_sip_listener(refresher->transaction->base.provider,BELLE_SIP_LISTENER(refresher));
316
	belle_sip_object_unref(refresher->transaction);
317
	refresher->transaction=NULL;
318
	if (refresher->realm) belle_sip_free(refresher->realm);
jehan's avatar
jehan committed
319
	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
320
}
321

322 323 324 325 326 327 328 329 330 331 332
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
333 334 335 336 337 338 339

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
340

341
int belle_sip_refresher_refresh(belle_sip_refresher_t* refresher,int expires) {
342 343 344
	/*first cancel any current retry*/
	cancel_retry(refresher);
	refresher->auth_failures=0;/*reset the auth_failures to get a chance to authenticate again*/
345
	return belle_sip_refresher_refresh_internal(refresher,expires,FALSE,NULL,NULL);
346
}
347

348 349 350 351
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
352

353
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
354
	belle_sip_request_t*old_request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
355
	belle_sip_response_t*old_response=belle_sip_transaction_get_response(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
356 357 358
	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
359
	belle_sip_header_expires_t* expires_header;
360
	belle_sip_uri_t* preset_route=refresher->transaction->preset_route;
jehan's avatar
jehan committed
361
	belle_sip_provider_t* prov=refresher->transaction->base.provider;
362
	belle_sip_header_contact_t* contact;
363

364
	/*first remove timer if any*/
jehan's avatar
jehan committed
365
	if (expires >=0) {
Simon Morlat's avatar
Simon Morlat committed
366
		refresher->target_expires=expires;
jehan's avatar
jehan committed
367 368 369
	} else {
		/*-1 keep last value*/
	}
jehan's avatar
jehan committed
370

jehan's avatar
jehan committed
371
	if (!dialog) {
jehan's avatar
jehan committed
372
		const belle_sip_transaction_state_t state=belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
373
		/*create new request*/
jehan's avatar
jehan committed
374 375 376
		if (belle_sip_transaction_state_is_transient(state)) {
			/*operation pending, cannot update authorization headers*/
			belle_sip_header_cseq_t* cseq;
Simon Morlat's avatar
Simon Morlat committed
377
			belle_sip_message("Refresher [%p] already has transaction [%p] in state [%s]"	,refresher
Simon Morlat's avatar
Simon Morlat committed
378 379
				,refresher->transaction
				,belle_sip_transaction_state_to_string(state));
380
			request=belle_sip_request_clone_with_body(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction)));
jehan's avatar
jehan committed
381 382 383
			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 {
384
			request=belle_sip_client_transaction_create_authenticated_request(refresher->transaction,auth_infos,refresher->realm);
jehan's avatar
jehan committed
385
		}
386 387 388 389 390 391 392
		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
393
	} else if (dialog && belle_sip_dialog_get_state(dialog)==BELLE_SIP_DIALOG_CONFIRMED) {
Simon Morlat's avatar
Simon Morlat committed
394 395 396 397
		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
398
		request=belle_sip_dialog_create_request_from(dialog,old_request);
jehan's avatar
jehan committed
399 400
		if (strcmp(belle_sip_request_get_method(request),"SUBSCRIBE")==0) {
			/*put expire header*/
jehan's avatar
jehan committed
401 402 403 404
			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
405
		}
406
		belle_sip_provider_add_authorization(prov,request,old_response,NULL,auth_infos,refresher->realm);
jehan's avatar
jehan committed
407 408 409 410 411 412 413
	} 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;
	}
414

415
	if (auth_mandatory && auth_infos && belle_sip_list_find_custom(*auth_infos, unfilled_auth_info, NULL)) {
416 417 418 419
		belle_sip_message("Auth info not found for this refresh operation on [%p]",refresher);
		if (request) belle_sip_object_unref(request);
		return -1;
	}
420

421
	refresher->on_io_error=0; /*reset this flag*/
422

423 424 425
	/*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
426
		belle_sip_header_expires_set_expires(expires_header,refresher->target_expires);
427
	contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t);
jehan's avatar
jehan committed
428
	if (contact && belle_sip_header_contact_get_expires(contact)>=0)
Simon Morlat's avatar
Simon Morlat committed
429
		belle_sip_header_contact_set_expires(contact,refresher->target_expires);
430

431 432 433 434 435 436 437 438
	/*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);
		}
	}
439

jehan's avatar
jehan committed
440 441 442
	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);
443 444 445 446 447 448 449 450 451 452 453

	switch (belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction))) {
	case BELLE_SIP_TRANSACTION_INIT:
	case BELLE_SIP_TRANSACTION_CALLING:
	case BELLE_SIP_TRANSACTION_TRYING:
		/*very early state, we can assume nobody will answer, stop retransmiting*/
		belle_sip_transaction_terminate(BELLE_SIP_TRANSACTION(refresher->transaction));
		break;
	default: /*we preserve the transaction "as is"*/
		break;
	}
jehan's avatar
jehan committed
454 455 456
	/*update reference transaction for next refresh*/
	belle_sip_object_unref(refresher->transaction);
	refresher->transaction=client_transaction;
jehan's avatar
jehan committed
457
	belle_sip_object_ref(refresher->transaction);
jehan's avatar
jehan committed
458

jehan's avatar
jehan committed
459
	if (belle_sip_client_transaction_send_request_to(client_transaction,requri?requri:preset_route)) { /*send imediatly to requri in case of redirect*/
jehan's avatar
jehan committed
460
		belle_sip_error("Cannot send refresh method [%s] for refresher [%p]"
461
				,belle_sip_request_get_method(request)
jehan's avatar
jehan committed
462
				,refresher);
Simon Morlat's avatar
Simon Morlat committed
463
		return -1;
jehan's avatar
jehan committed
464
	}
465
	if (expires==0) belle_sip_refresher_stop_internal(refresher,0); /*unregister transaction must be preserved*/
jehan's avatar
jehan committed
466 467 468
	return 0;
}

469

jehan's avatar
jehan committed
470 471
static int timer_cb(void *user_data, unsigned int events) {
	belle_sip_refresher_t* refresher = (belle_sip_refresher_t*)user_data;
472

473 474 475 476 477 478
	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;
	}
479

Simon Morlat's avatar
Simon Morlat committed
480 481 482
	if (belle_sip_refresher_refresh(refresher,refresher->target_expires)==-1){
		retry_later(refresher);
	}
483
	return BELLE_SIP_STOP;
jehan's avatar
jehan committed
484
}
485

jehan's avatar
jehan committed
486 487 488 489
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
490 491 492 493
	belle_sip_header_contact_t* unfixed_local_contact;
	belle_sip_header_contact_t* fixed_local_contact;
	char* tmp_string;
	char* tmp_string2;
494 495
	if (!response)
		return NULL;
jehan's avatar
jehan committed
496
	/*we assume, there is only one contact in request*/
jehan's avatar
jehan committed
497 498
	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
499 500

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

jehan's avatar
jehan committed
504
	if (contact_header_list) {
Simon Morlat's avatar
Simon Morlat committed
505 506 507 508 509 510
		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
511
			contact_header_list = belle_sip_list_find_custom((belle_sip_list_t*)contact_header_list
Simon Morlat's avatar
Simon Morlat committed
512 513
							,(belle_sip_compare_func)belle_sip_header_contact_not_equals
							,unfixed_local_contact);
Simon Morlat's avatar
Simon Morlat committed
514 515 516 517 518 519 520 521
		}
		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
522
		} else {
Simon Morlat's avatar
Simon Morlat committed
523 524 525
			return BELLE_SIP_HEADER_CONTACT(contact_header_list->data);
		}
	} else {
jehan's avatar
jehan committed
526 527 528 529 530 531 532 533
		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
534
	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
535
	belle_sip_header_contact_t* contact_header;
536

Simon Morlat's avatar
Simon Morlat committed
537
	refresher->obtained_expires=-1;
538

jehan's avatar
jehan committed
539
	if (strcmp("REGISTER",belle_sip_request_get_method(request))==0
jehan's avatar
jehan committed
540
			|| expires_header /*if request has an expire header, refresher can always work*/) {
jehan's avatar
jehan committed
541

Simon Morlat's avatar
Simon Morlat committed
542 543
		if (expires_header)
			refresher->target_expires = belle_sip_header_expires_get_expires(expires_header);
544

jehan's avatar
jehan committed
545 546 547 548
		/*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
549 550 551 552 553 554 555 556 557 558
		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));
			}
559
		}
Simon Morlat's avatar
Simon Morlat committed
560
		if (refresher->obtained_expires==-1){
jehan's avatar
jehan committed
561
			/*no contact with expire or not relevant, looking for Expires header*/
562
			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
563
				refresher->obtained_expires = belle_sip_header_expires_get_expires(expires_header);
jehan's avatar
jehan committed
564 565
			}
		}
566
		if (refresher->obtained_expires==-1) {
567
			belle_sip_message("Neither Expires header nor corresponding Contact header found, checking from original request");
Simon Morlat's avatar
Simon Morlat committed
568
			refresher->obtained_expires=refresher->target_expires;
569 570 571 572 573
		}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
574
		}
Simon Morlat's avatar
Simon Morlat committed
575 576
	} else if (strcmp("INVITE",belle_sip_request_get_method(request))==0) {
		belle_sip_error("Refresher does not support INVITE yet");
577
		return -1;
jehan's avatar
jehan committed
578 579
	} else {
		belle_sip_error("Refresher does not support [%s] yet",belle_sip_request_get_method(request));
580
		return -1;
jehan's avatar
jehan committed
581 582 583 584 585 586
	}
	return 0;
}


int belle_sip_refresher_start(belle_sip_refresher_t* refresher) {
jehan's avatar
jehan committed
587
	if(refresher->state==started) {
Simon Morlat's avatar
Simon Morlat committed
588
		belle_sip_warning("Refresher [%p] already started",refresher);
jehan's avatar
jehan committed
589
	} else {
Simon Morlat's avatar
Simon Morlat committed
590
		if (refresher->target_expires>0) {
591
			belle_sip_request_t* request = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction));
Simon Morlat's avatar
Simon Morlat committed
592
			refresher->state=started;
593
			if (is_contact_address_acurate(refresher,request)) {
594 595 596 597 598 599
				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
600
			belle_sip_message("Refresher [%p] started, next refresh in [%i] s",refresher,refresher->obtained_expires);
Simon Morlat's avatar
Simon Morlat committed
601
		}else{
Simon Morlat's avatar
Simon Morlat committed
602
			belle_sip_message("Refresher [%p] stopped, expires=%i",refresher,refresher->target_expires);
jehan's avatar
jehan committed
603
			refresher->state=stopped;
Simon Morlat's avatar
Simon Morlat committed
604
		}
jehan's avatar
jehan committed
605 606 607 608
	}
	return 0;
}
void belle_sip_refresher_stop(belle_sip_refresher_t* refresher) {
609 610 611 612
	belle_sip_refresher_stop_internal(refresher, 1);
}

static void belle_sip_refresher_stop_internal(belle_sip_refresher_t* refresher,int cancel_pending_transaction) {
Simon Morlat's avatar
Simon Morlat committed
613
	belle_sip_message("Refresher [%p] stopped.",refresher);
614 615 616 617 618
	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;
	}
619
	if (cancel_pending_transaction && refresher->transaction && belle_sip_transaction_state_is_transient(belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)))) {
620 621
		belle_sip_transaction_terminate(BELLE_SIP_TRANSACTION(refresher->transaction)); /*refresher cancelled, no need to continue to retransmit*/
	}
jehan's avatar
jehan committed
622
	refresher->state=stopped;
jehan's avatar
jehan committed
623
}
Simon Morlat's avatar
Simon Morlat committed
624

jehan's avatar
jehan committed
625
belle_sip_refresher_t* belle_sip_refresher_new(belle_sip_client_transaction_t* transaction) {
626
	belle_sip_refresher_t* refresher;
627
	belle_sip_transaction_state_t state=belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(transaction));
jehan's avatar
jehan committed
628
	belle_sip_request_t* request = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(transaction));
629
	int is_register=strcmp("REGISTER",belle_sip_request_get_method(request))==0;
630

631
	refresher = (belle_sip_refresher_t*)belle_sip_object_new(belle_sip_refresher_t);
jehan's avatar
jehan committed
632
	refresher->transaction=transaction;
jehan's avatar
jehan committed
633
	refresher->state=stopped;
634
	refresher->number_of_retry=0;
jehan's avatar
jehan committed
635
	belle_sip_object_ref(transaction);
jehan's avatar
jehan committed
636
	refresher->retry_after=DEFAULT_RETRY_AFTER;
637

638
	belle_sip_provider_add_internal_sip_listener(transaction->base.provider,BELLE_SIP_LISTENER(refresher), is_register);
Simon Morlat's avatar
Simon Morlat committed
639
	if (set_expires_from_trans(refresher)==-1){
jehan's avatar
jehan committed
640 641
		belle_sip_error("Unable to extract refresh value from transaction [%p]",transaction);
	}
642
	if (belle_sip_transaction_state_is_transient(state)) {
643
		belle_sip_message("Refresher [%p] takes ownership of transaction [%p]",refresher,transaction);
644
		transaction->base.is_internal=1;
Simon Morlat's avatar
Simon Morlat committed
645
		refresher->state=started;
646 647
	}else{
		belle_sip_refresher_start(refresher);
648
	}
jehan's avatar
jehan committed
649 650
	return refresher;
}
Simon Morlat's avatar
Simon Morlat committed
651

652
int belle_sip_refresher_get_expires(const belle_sip_refresher_t* refresher) {
Simon Morlat's avatar
Simon Morlat committed
653
	return refresher->target_expires;
654
}
jehan's avatar
jehan committed
655 656 657 658 659 660 661 662

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;
}
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677

const char* belle_sip_refresher_get_realm(const belle_sip_refresher_t* refresher){
	return refresher->realm;
}

void belle_sip_refresher_set_realm(belle_sip_refresher_t* refresher, const char* realm) {
	if (refresher->realm){
		belle_sip_free(refresher->realm);
		refresher->realm = NULL;
	}
	if (realm!=NULL){
		refresher->realm=belle_sip_strdup(realm);
	}
}

jehan's avatar
jehan committed
678 679 680 681 682 683
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;
}
684

685 686 687 688
void belle_sip_refresher_enable_manual_mode(belle_sip_refresher_t *refresher, int enabled){
	refresher->manual=enabled;
}

689 690 691
char *belle_sip_refresher_get_public_uri(belle_sip_refresher_t* refresher)  {
	return belle_sip_channel_get_public_ip_port(refresher->transaction->base.channel);
}