refresher.c 24.6 KB
Newer Older
jehan's avatar
jehan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 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
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "belle_sip_internal.h"
#include "belle-sip/refresher-helper.h"

jehan's avatar
jehan committed
22
#define DEFAULT_RETRY_AFTER 60000
jehan's avatar
jehan committed
23 24 25 26
typedef enum belle_sip_refresher_state {
	started,
	stopped
}belle_sip_refresher_state_t;
Simon Morlat's avatar
Simon Morlat committed
27

jehan's avatar
jehan committed
28 29 30 31 32
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
33 34
	int target_expires;
	int obtained_expires;
jehan's avatar
jehan committed
35
	belle_sip_refresher_state_t state;
jehan's avatar
jehan committed
36
	belle_sip_listener_callbacks_t listener_callbacks;
Simon Morlat's avatar
Simon Morlat committed
37
	belle_sip_listener_t *sip_listener;
jehan's avatar
jehan committed
38
	void* user_data;
jehan's avatar
jehan committed
39
	int retry_after;
jehan's avatar
jehan committed
40 41 42
	belle_sip_list_t* auth_events;
	belle_sip_header_contact_t* nated_contact;
	int enable_nat_helper;
43
	int auth_failures;
jehan's avatar
jehan committed
44
	int on_io_error; /*flag to avoid multiple error notification*/
jehan's avatar
jehan committed
45
};
jehan's avatar
jehan committed
46 47 48
static int set_expires_from_trans(belle_sip_refresher_t* refresher);

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

jehan's avatar
jehan committed
51

jehan's avatar
jehan committed
52
static void schedule_timer_at(belle_sip_refresher_t* refresher,int delay) {
Simon Morlat's avatar
Simon Morlat committed
53 54 55 56
	belle_sip_message("Refresher: scheduling next timer in %i ms",delay);
	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
57
	}
Simon Morlat's avatar
Simon Morlat committed
58 59 60
	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
61 62

}
63
static void retry_later(belle_sip_refresher_t* refresher) {
jehan's avatar
jehan committed
64 65
	schedule_timer_at(refresher,refresher->retry_after);
}
Simon Morlat's avatar
Simon Morlat committed
66

jehan's avatar
jehan committed
67
static void schedule_timer(belle_sip_refresher_t* refresher) {
Simon Morlat's avatar
Simon Morlat committed
68
	schedule_timer_at(refresher,refresher->obtained_expires*900);
jehan's avatar
jehan committed
69 70
}

jehan's avatar
jehan committed
71 72
static void process_dialog_terminated(void *user_ctx, const belle_sip_dialog_terminated_event_t *event){
	/*nop*/
jehan's avatar
jehan committed
73 74
}
static void process_io_error(void *user_ctx, const belle_sip_io_error_event_t *event){
75 76
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
	belle_sip_client_transaction_t*client_transaction;
jehan's avatar
jehan committed
77 78 79
	if (refresher->on_io_error==1) {
		return; /*refresher already on error*/
	}
Simon Morlat's avatar
Simon Morlat committed
80
	if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event),belle_sip_client_transaction_t)) {
81
		client_transaction=BELLE_SIP_CLIENT_TRANSACTION(belle_sip_io_error_event_get_source(event));
82
		if (!refresher || (refresher && ((refresher->state==stopped
Simon Morlat's avatar
Simon Morlat committed
83 84 85
			&& 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 )))
86
				return; /*not for me or no longuer involved*/
87

Simon Morlat's avatar
Simon Morlat committed
88
		if (refresher->target_expires==0
89 90 91 92
				&& 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*/
		}
Simon Morlat's avatar
Simon Morlat committed
93
		if (refresher->state==started) retry_later(refresher);
jehan's avatar
jehan committed
94
		if (refresher->listener) refresher->listener(refresher,refresher->user_data,503, "io error");
jehan's avatar
jehan committed
95
		refresher->on_io_error=1;
96
		return;
Simon Morlat's avatar
Simon Morlat committed
97
	} else if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(belle_sip_io_error_event_get_source(event),belle_sip_provider_t)) {
98
		/*something went wrong on this provider, checking if my channel is still up*/
jehan's avatar
jehan committed
99
		if (refresher->state==started  /*refresher started or trying to refresh */
jehan's avatar
jehan committed
100 101
				&& belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction)) == BELLE_SIP_TRANSACTION_TERMINATED /*else we are notified by transaction error*/
				&&	(belle_sip_channel_get_state(refresher->transaction->base.channel) == BELLE_SIP_CHANNEL_DISCONNECTED
102 103 104 105 106
								||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)));
Simon Morlat's avatar
Simon Morlat committed
107
			if (refresher->state==started) retry_later(refresher);
jehan's avatar
jehan committed
108
			if (refresher->listener) refresher->listener(refresher,refresher->user_data,503, "io error");
jehan's avatar
jehan committed
109
			refresher->on_io_error=1;
110 111 112
		}
		return;
	}else {
jehan's avatar
jehan committed
113 114
		/*belle_sip_error("Refresher process_io_error not implemented yet for non transaction/provider source");*/
		/*nop, because already handle at transaction layer*/
115
	}
jehan's avatar
jehan committed
116 117 118 119 120 121 122
}



static void process_response_event(void *user_ctx, const belle_sip_response_event_t *event){
	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
123
	belle_sip_request_t* request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(client_transaction));
jehan's avatar
jehan committed
124 125
	int response_code = belle_sip_response_get_status_code(response);
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
jehan's avatar
jehan committed
126 127
	belle_sip_header_contact_t* contact_header;

jehan's avatar
jehan committed
128 129 130
	if (refresher && (client_transaction !=refresher->transaction))
		return; /*not for me*/

jehan's avatar
jehan committed
131 132 133 134 135 136 137 138
	if ((contact_header=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t))) {
		if (refresher->nated_contact) belle_sip_object_unref(refresher->nated_contact);
		refresher->nated_contact=BELLE_SIP_HEADER_CONTACT(belle_sip_object_clone(BELLE_SIP_OBJECT(contact_header)));
		belle_sip_object_ref(refresher->nated_contact);
		belle_sip_response_fix_contact(response,refresher->nated_contact);
	}
	/*handle authorization*/
	switch (response_code) {
139 140
	case 200:
		refresher->auth_failures=0;
jehan's avatar
jehan committed
141
		/*great, success*/
jehan's avatar
jehan committed
142 143 144 145 146 147 148 149 150
		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 {
151
				belle_sip_warning("Refresher [%p] receive 200ok to a publish without etag",refresher);
jehan's avatar
jehan committed
152 153
			}
		}
jehan's avatar
jehan committed
154 155
		/*update expire if needed*/
		set_expires_from_trans(refresher);
Simon Morlat's avatar
Simon Morlat committed
156
		if (refresher->target_expires<=0) {
157 158
			belle_sip_refresher_stop(refresher); /*doesn not make sens to refresh if expire =0;*/
		}
Simon Morlat's avatar
Simon Morlat committed
159
		if (refresher->state==started) schedule_timer(refresher); /*re-arm timer*/
160
		else belle_sip_message("Refresher [%p] not scheduling next refresh, because it was stopped",refresher);
jehan's avatar
jehan committed
161 162
		break;
	case 401:
163 164 165 166
	case 407:
		refresher->auth_failures++;
		if (refresher->auth_failures>3){
			/*avoid looping with 407 or 401 */
167
			belle_sip_warning("Authentication is failing constantly, giving up.");
Simon Morlat's avatar
Simon Morlat committed
168
			if (refresher->target_expires>0) retry_later(refresher);
169 170
			break;
		}
jehan's avatar
jehan committed
171 172
		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
173
		}
Simon Morlat's avatar
Simon Morlat committed
174
		if (belle_sip_refresher_refresh_internal(refresher,refresher->target_expires,TRUE,&refresher->auth_events))
jehan's avatar
jehan committed
175 176 177
			break; /*Notify user of registration failure*/
		else
			return; /*ok, keep 401 internal*/
178 179 180 181 182 183
	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
184
				if (new_expires>0 && refresher->state==started){
Simon Morlat's avatar
Simon Morlat committed
185 186
					refresher->target_expires=new_expires;
					belle_sip_refresher_refresh(refresher,refresher->target_expires);
187 188 189 190 191
					return;
				}
			}
		}else belle_sip_warning("Receiving 423 but no min-expires header.");
		break;
jehan's avatar
jehan committed
192 193 194 195 196
	}
	case 408:
	case 480:
	case 503:
	case 504:
Simon Morlat's avatar
Simon Morlat committed
197
		if (refresher->target_expires>0) retry_later(refresher);
jehan's avatar
jehan committed
198 199 200 201 202
		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
203 204 205

}
static void process_timeout(void *user_ctx, const belle_sip_timeout_event_t *event) {
jehan's avatar
jehan committed
206
	belle_sip_refresher_t* refresher=(belle_sip_refresher_t*)user_ctx;
jehan's avatar
jehan committed
207 208
	belle_sip_client_transaction_t*client_transaction =belle_sip_timeout_event_get_client_transaction(event);

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

Simon Morlat's avatar
Simon Morlat committed
212
	if (refresher->state==started) {
Simon Morlat's avatar
Simon Morlat committed
213 214 215
		/*retry in 2 seconds but not immediately to let the current transaction be cleaned*/
		schedule_timer_at(refresher,2000);
	}
jehan's avatar
jehan committed
216
	if (refresher->listener) refresher->listener(refresher,refresher->user_data,408, "timeout");
jehan's avatar
jehan committed
217
}
Simon Morlat's avatar
Simon Morlat committed
218

jehan's avatar
jehan committed
219
static void process_transaction_terminated(void *user_ctx, const belle_sip_transaction_terminated_event_t *event) {
jehan's avatar
jehan committed
220
	/*belle_sip_message("process_transaction_terminated Transaction terminated [%p]",event);*/
jehan's avatar
jehan committed
221 222 223
}

static void destroy(belle_sip_refresher_t *refresher){
224
	belle_sip_refresher_stop(refresher);
225 226
	belle_sip_provider_remove_internal_sip_listener(refresher->transaction->base.provider,refresher->sip_listener);
	belle_sip_object_unref(refresher->transaction);
227
	refresher->transaction=NULL;
228
	belle_sip_object_unref(refresher->sip_listener);
229
	refresher->sip_listener=NULL;
jehan's avatar
jehan committed
230 231 232
	if (refresher->auth_events) refresher->auth_events=belle_sip_list_free_with_data(refresher->auth_events,(void (*)(void*))belle_sip_auth_event_destroy);
	if (refresher->nated_contact) belle_sip_object_unref(refresher->nated_contact);

jehan's avatar
jehan committed
233
}
234

jehan's avatar
jehan committed
235 236 237 238 239 240 241 242
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_refresher_t);

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
243

244
int belle_sip_refresher_refresh(belle_sip_refresher_t* refresher,int expires) {
245 246
	return belle_sip_refresher_refresh_internal(refresher,expires,FALSE,NULL);
}
247 248 249 250
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
251

252
static int belle_sip_refresher_refresh_internal(belle_sip_refresher_t* refresher,int expires,int auth_mandatory, belle_sip_list_t** auth_infos) {
jehan's avatar
jehan committed
253
	belle_sip_request_t*old_request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
254
	belle_sip_response_t*old_response=belle_sip_transaction_get_response(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
255 256 257
	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
258
	belle_sip_header_expires_t* expires_header;
259
	belle_sip_uri_t* preset_route=refresher->transaction->preset_route;
jehan's avatar
jehan committed
260
	belle_sip_provider_t* prov=refresher->transaction->base.provider;
261
	belle_sip_header_contact_t* contact;
Simon Morlat's avatar
Simon Morlat committed
262
	
263
	/*first remove timer if any*/
jehan's avatar
jehan committed
264
	if (expires >=0) {
Simon Morlat's avatar
Simon Morlat committed
265
		refresher->target_expires=expires;
jehan's avatar
jehan committed
266 267 268
	} else {
		/*-1 keep last value*/
	}
jehan's avatar
jehan committed
269 270
	refresher->on_io_error=0; /*reset this flag*/

jehan's avatar
jehan committed
271
	if (!dialog) {
jehan's avatar
jehan committed
272
		const belle_sip_transaction_state_t state=belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(refresher->transaction));
jehan's avatar
jehan committed
273
		/*create new request*/
jehan's avatar
jehan committed
274 275 276
		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
277
			belle_sip_message("Refresher [%p] already have transaction [%p] in state [%s]"	,refresher
Simon Morlat's avatar
Simon Morlat committed
278 279
				,refresher->transaction
				,belle_sip_transaction_state_to_string(state));
280
			request=belle_sip_request_clone_with_body(belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(refresher->transaction)));
jehan's avatar
jehan committed
281 282 283
			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 {
284
			request=belle_sip_client_transaction_create_authenticated_request(refresher->transaction,auth_infos);
jehan's avatar
jehan committed
285
		}
jehan's avatar
jehan committed
286
	} else if (dialog && belle_sip_dialog_get_state(dialog)==BELLE_SIP_DIALOG_CONFIRMED) {
Simon Morlat's avatar
Simon Morlat committed
287 288 289 290
		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
291
		request=belle_sip_dialog_create_request_from(dialog,old_request);
jehan's avatar
jehan committed
292 293
		if (strcmp(belle_sip_request_get_method(request),"SUBSCRIBE")==0) {
			/*put expire header*/
jehan's avatar
jehan committed
294 295 296 297
			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
298
		}
299
		belle_sip_provider_add_authorization(prov,request,old_response,auth_infos);
jehan's avatar
jehan committed
300 301 302 303 304 305 306
	} 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;
	}
307

308
	if (auth_mandatory && auth_infos && belle_sip_list_find_custom(*auth_infos, unfilled_auth_info, NULL)) {
309 310 311 312
		belle_sip_message("Auth info not found for this refresh operation on [%p]",refresher);
		if (request) belle_sip_object_unref(request);
		return -1;
	}
jehan's avatar
jehan committed
313 314 315 316 317
	if (refresher->enable_nat_helper && refresher->nated_contact) {
		/*update contact with fixed contact*/
		belle_sip_message_remove_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_CONTACT);
		belle_sip_message_add_header(BELLE_SIP_MESSAGE(request),BELLE_SIP_HEADER(refresher->nated_contact));
	}
318 319 320
	/*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
321
		belle_sip_header_expires_set_expires(expires_header,refresher->target_expires);
322
	contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t);
jehan's avatar
jehan committed
323
	if (contact && belle_sip_header_contact_get_expires(contact)>=0)
Simon Morlat's avatar
Simon Morlat committed
324
		belle_sip_header_contact_set_expires(contact,refresher->target_expires);
325

326 327 328 329 330 331 332 333 334
	/*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
335 336 337 338 339 340
	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
341
	belle_sip_object_ref(refresher->transaction);
jehan's avatar
jehan committed
342

343
	if (belle_sip_client_transaction_send_request_to(client_transaction,preset_route)) {
jehan's avatar
jehan committed
344
		belle_sip_error("Cannot send refresh method [%s] for refresher [%p]"
345
				,belle_sip_request_get_method(request)
jehan's avatar
jehan committed
346
				,refresher);
Simon Morlat's avatar
Simon Morlat committed
347
		return -1;
jehan's avatar
jehan committed
348
	}
349
	if (expires==0) belle_sip_refresher_stop(refresher);
jehan's avatar
jehan committed
350 351 352
	return 0;
}

353

jehan's avatar
jehan committed
354 355
static int timer_cb(void *user_data, unsigned int events) {
	belle_sip_refresher_t* refresher = (belle_sip_refresher_t*)user_data;
356
	refresher->auth_failures=0;/*reset the auth_failures to get a chance to authenticate again*/
Simon Morlat's avatar
Simon Morlat committed
357 358 359
	if (belle_sip_refresher_refresh(refresher,refresher->target_expires)==-1){
		retry_later(refresher);
	}
360
	return BELLE_SIP_STOP;
jehan's avatar
jehan committed
361
}
362

jehan's avatar
jehan committed
363 364 365 366
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
367 368 369 370
	belle_sip_header_contact_t* unfixed_local_contact;
	belle_sip_header_contact_t* fixed_local_contact;
	char* tmp_string;
	char* tmp_string2;
371 372
	if (!response)
		return NULL;
jehan's avatar
jehan committed
373
	/*we assume, there is only one contact in request*/
jehan's avatar
jehan committed
374 375
	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
376 377

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

jehan's avatar
jehan committed
381
	if (contact_header_list) {
Simon Morlat's avatar
Simon Morlat committed
382 383 384 385 386 387
		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
388
			contact_header_list = belle_sip_list_find_custom((belle_sip_list_t*)contact_header_list
Simon Morlat's avatar
Simon Morlat committed
389 390
							,(belle_sip_compare_func)belle_sip_header_contact_not_equals
							,unfixed_local_contact);
Simon Morlat's avatar
Simon Morlat committed
391 392 393 394 395 396 397 398
		}
		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
399
		} else {
Simon Morlat's avatar
Simon Morlat committed
400 401 402
			return BELLE_SIP_HEADER_CONTACT(contact_header_list->data);
		}
	} else {
jehan's avatar
jehan committed
403 404 405 406 407 408 409 410
		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
411
	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
412
	belle_sip_header_contact_t* contact_header;
413

Simon Morlat's avatar
Simon Morlat committed
414
	refresher->obtained_expires=-1;
415
	
jehan's avatar
jehan committed
416
	if (strcmp("REGISTER",belle_sip_request_get_method(request))==0
jehan's avatar
jehan committed
417
			|| expires_header /*if request has an expire header, refresher can always work*/) {
jehan's avatar
jehan committed
418

Simon Morlat's avatar
Simon Morlat committed
419 420 421
		if (expires_header)
			refresher->target_expires = belle_sip_header_expires_get_expires(expires_header);
		
jehan's avatar
jehan committed
422 423 424 425
		/*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
426 427 428 429 430 431 432 433 434 435
		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));
			}
436
		}
Simon Morlat's avatar
Simon Morlat committed
437
		if (refresher->obtained_expires==-1){
jehan's avatar
jehan committed
438
			/*no contact with expire or not relevant, looking for Expires header*/
439
			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
440
				refresher->obtained_expires = belle_sip_header_expires_get_expires(expires_header);
jehan's avatar
jehan committed
441 442
			}
		}
Simon Morlat's avatar
Simon Morlat committed
443
		if (refresher->obtained_expires<0) {
444
			belle_sip_message("Neither Expires header nor corresponding Contact header found, checking from original request");
Simon Morlat's avatar
Simon Morlat committed
445
			refresher->obtained_expires=refresher->target_expires;
jehan's avatar
jehan committed
446
		}
Simon Morlat's avatar
Simon Morlat committed
447 448
	} else if (strcmp("INVITE",belle_sip_request_get_method(request))==0) {
		belle_sip_error("Refresher does not support INVITE yet");
449
		return -1;
jehan's avatar
jehan committed
450 451
	} else {
		belle_sip_error("Refresher does not support [%s] yet",belle_sip_request_get_method(request));
452
		return -1;
jehan's avatar
jehan committed
453 454 455 456 457 458
	}
	return 0;
}


int belle_sip_refresher_start(belle_sip_refresher_t* refresher) {
jehan's avatar
jehan committed
459
	if(refresher->state==started) {
Simon Morlat's avatar
Simon Morlat committed
460
		belle_sip_warning("Refresher [%p] already started",refresher);
jehan's avatar
jehan committed
461
	} else {
Simon Morlat's avatar
Simon Morlat committed
462
		if (refresher->target_expires>0) {
Simon Morlat's avatar
Simon Morlat committed
463
			refresher->state=started;
jehan's avatar
jehan committed
464
			schedule_timer(refresher);
Simon Morlat's avatar
Simon Morlat committed
465
			belle_sip_message("Refresher [%p] started, next refresh in [%i] s",refresher,refresher->obtained_expires);
Simon Morlat's avatar
Simon Morlat committed
466
		}else{
Simon Morlat's avatar
Simon Morlat committed
467
			belle_sip_message("Refresher [%p] stopped, expires=%i",refresher,refresher->target_expires);
jehan's avatar
jehan committed
468
			refresher->state=stopped;
Simon Morlat's avatar
Simon Morlat committed
469
		}
jehan's avatar
jehan committed
470 471 472 473 474
	}
	return 0;
}

void belle_sip_refresher_stop(belle_sip_refresher_t* refresher) {
Simon Morlat's avatar
Simon Morlat committed
475
	belle_sip_message("Refresher [%p] stopped.",refresher);
476 477 478 479 480
	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
481
	refresher->state=stopped;
jehan's avatar
jehan committed
482
}
Simon Morlat's avatar
Simon Morlat committed
483

jehan's avatar
jehan committed
484
belle_sip_refresher_t* belle_sip_refresher_new(belle_sip_client_transaction_t* transaction) {
485
	belle_sip_refresher_t* refresher;
486
	belle_sip_transaction_state_t state=belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(transaction));
jehan's avatar
jehan committed
487 488
	belle_sip_request_t* request = belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(transaction));
	if ( strcmp("REGISTER",belle_sip_request_get_method(request))!=0
jehan's avatar
jehan committed
489
			&& strcmp("PUBLISH",belle_sip_request_get_method(request))!=0
490
			&& state!=BELLE_SIP_TRANSACTION_TERMINATED
jehan's avatar
jehan committed
491 492
			&& state != BELLE_SIP_TRANSACTION_COMPLETED) {
		belle_sip_error("Invalid state [%s] for %s transaction [%p], should be BELLE_SIP_TRANSACTION_COMPLETED/BELLE_SIP_TRANSACTION_TERMINATED"
jehan's avatar
jehan committed
493
					,belle_sip_transaction_state_to_string(belle_sip_transaction_get_state(BELLE_SIP_TRANSACTION(transaction)))
jehan's avatar
jehan committed
494
					,belle_sip_request_get_method(request)
jehan's avatar
jehan committed
495 496 497
					,transaction);
		return NULL;
	}
498
	refresher = (belle_sip_refresher_t*)belle_sip_object_new(belle_sip_refresher_t);
jehan's avatar
jehan committed
499
	refresher->transaction=transaction;
jehan's avatar
jehan committed
500 501
	refresher->state=stopped;
	refresher->enable_nat_helper=1;
jehan's avatar
jehan committed
502
	belle_sip_object_ref(transaction);
jehan's avatar
jehan committed
503
	refresher->retry_after=DEFAULT_RETRY_AFTER;
jehan's avatar
jehan committed
504 505 506 507
	refresher->listener_callbacks.process_response_event=process_response_event;
	refresher->listener_callbacks.process_timeout=process_timeout;
	refresher->listener_callbacks.process_io_error=process_io_error;
	refresher->listener_callbacks.process_dialog_terminated=process_dialog_terminated;
Simon Morlat's avatar
Simon Morlat committed
508 509 510
	refresher->listener_callbacks.process_transaction_terminated=process_transaction_terminated;;
	refresher->sip_listener=belle_sip_listener_create_from_callbacks(&(refresher->listener_callbacks),refresher);
	belle_sip_provider_add_internal_sip_listener(transaction->base.provider,refresher->sip_listener);
Simon Morlat's avatar
Simon Morlat committed
511
	if (set_expires_from_trans(refresher)==-1){
jehan's avatar
jehan committed
512 513
		belle_sip_error("Unable to extract refresh value from transaction [%p]",transaction);
	}
514 515 516
	if (belle_sip_transaction_state_is_transient(state)) {
		belle_sip_message(" refresher [%p] takes ownership of transaction [%p]",refresher,transaction);
		transaction->base.is_internal=1;
Simon Morlat's avatar
Simon Morlat committed
517
		refresher->state=started;
518
	}
jehan's avatar
jehan committed
519 520
	return refresher;
}
Simon Morlat's avatar
Simon Morlat committed
521

522
int belle_sip_refresher_get_expires(const belle_sip_refresher_t* refresher) {
Simon Morlat's avatar
Simon Morlat committed
523
	return refresher->target_expires;
524
}
jehan's avatar
jehan committed
525 526 527 528 529 530 531 532

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
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
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;
}
const belle_sip_header_contact_t* belle_sip_refresher_get_nated_contact(const belle_sip_refresher_t* refresher) {
	return refresher->nated_contact;
}
void belle_sip_refresher_enable_nat_helper(belle_sip_refresher_t* refresher,int enable) {
	refresher->enable_nat_helper=enable;
}
int belle_sip_refresher_is_nat_helper_enabled(const belle_sip_refresher_t* refresher) {
	return refresher->enable_nat_helper;
}