transaction.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
	belle-sip - SIP (RFC3261) library.
    Copyright (C) 2010  Belledonne Communications SARL

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

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

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

#include "belle_sip_internal.h"
20
#include "sender_task.h"
21 22 23



Simon Morlat's avatar
Simon Morlat committed
24
static belle_sip_source_t * transaction_create_timer(belle_sip_transaction_t *t, belle_sip_source_func_t func, unsigned int time_ms){
25
	belle_sip_stack_t *stack=belle_sip_provider_get_sip_stack(t->provider);
Simon Morlat's avatar
Simon Morlat committed
26 27 28
	belle_sip_source_t *s=belle_sip_timeout_source_new (func,t,time_ms);
	belle_sip_main_loop_add_source(stack->ml,s);
	return s;
29 30
}

31 32

static void transaction_delete_timer(belle_sip_transaction_t *t, belle_sip_source_t *s){
33
	belle_sip_stack_t *stack=belle_sip_provider_get_sip_stack(t->provider);
34 35
	belle_sip_main_loop_cancel_source (stack->ml,s->id);
	belle_sip_object_unref(s);
36
}
37

38 39 40 41 42 43 44 45 46 47

static void belle_sip_transaction_init(belle_sip_transaction_t *t, belle_sip_provider_t *prov, belle_sip_request_t *req){
	belle_sip_object_init_type(t,belle_sip_transaction_t);
	if (req) belle_sip_object_ref(req);
	t->request=req;
	t->provider=prov;
}

static void transaction_destroy(belle_sip_transaction_t *t){
	if (t->request) belle_sip_object_unref(t->request);
Simon Morlat's avatar
Simon Morlat committed
48 49
	if (t->prov_response) belle_sip_object_unref(t->prov_response);
	if (t->final_response) belle_sip_object_unref(t->final_response);
50 51
	if (t->stask) belle_sip_object_unref(t->stask);
}
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

void *belle_sip_transaction_get_application_data(const belle_sip_transaction_t *t){
	return t->appdata;
}

void belle_sip_transaction_set_application_data(belle_sip_transaction_t *t, void *data){
	t->appdata=data;
}

const char *belle_sip_transaction_get_branch_id(const belle_sip_transaction_t *t){
	return t->branch_id;
}

belle_sip_transaction_state_t belle_sip_transaction_get_state(const belle_sip_transaction_t *t){
	return t->state;
}

void belle_sip_transaction_terminate(belle_sip_transaction_t *t){
70 71 72 73 74 75
	t->state=BELLE_SIP_TRANSACTION_TERMINATED;
	if (t->timer){
		transaction_delete_timer(t,t->timer);
		t->timer=NULL;
	}
	belle_sip_provider_set_transaction_terminated(t->provider,t);
76 77 78 79 80 81
}

belle_sip_request_t *belle_sip_transaction_get_request(belle_sip_transaction_t *t){
	return t->request;
}

82
/*
83 84 85 86 87
 *
 *
 *	Server transaction
 *
 *
88 89 90 91 92 93
*/

struct belle_sip_server_transaction{
	belle_sip_transaction_t base;
};

94 95 96 97 98 99 100 101 102 103 104 105 106 107
static void server_transaction_send_cb(belle_sip_sender_task_t *st, void *data, int retcode){
	belle_sip_server_transaction_t *t=(belle_sip_server_transaction_t *)data;
	if (retcode==0){
	}else{
		/*the provider is notified of the error by the sender_task, we just need to terminate the transaction*/
		belle_sip_transaction_terminate(&t->base);
	}
}

void belle_sip_server_transaction_send_response(belle_sip_server_transaction_t *t, belle_sip_response_t *resp){
	if (t->base.stask==NULL){
		t->base.stask=belle_sip_sender_task_new(t->base.provider,server_transaction_send_cb,t);
	}
	belle_sip_sender_task_send(t->base.stask,BELLE_SIP_MESSAGE(resp));
108 109
}

110 111 112 113 114 115 116 117 118 119 120
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
	transaction_destroy((belle_sip_transaction_t*)t);
}

belle_sip_server_transaction_t * belle_sip_server_transaction_new(belle_sip_provider_t *prov,belle_sip_request_t *req){
	belle_sip_server_transaction_t *t=belle_sip_object_new(belle_sip_server_transaction_t,(belle_sip_object_destroy_t)server_transaction_destroy);
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
	return t;
}

/*
121 122 123 124 125
 *
 *
 *	Client transaction
 *
 *
126 127 128 129
*/

struct belle_sip_client_transaction{
	belle_sip_transaction_t base;
Simon Morlat's avatar
Simon Morlat committed
130 131 132
	uint64_t timer_F;
	uint64_t timer_E;
	uint64_t timer_K;
133 134
};

135 136 137 138
belle_sip_request_t * belle_sip_client_transaction_create_cancel(belle_sip_client_transaction_t *t){
	return NULL;
}

139
static int on_client_transaction_timer(void *data, unsigned int revents){
Simon Morlat's avatar
Simon Morlat committed
140 141
	belle_sip_client_transaction_t *t=(belle_sip_client_transaction_t*)data;
	const belle_sip_timer_config_t *tc=belle_sip_stack_get_timer_config (belle_sip_provider_get_sip_stack (t->base.provider));
Simon Morlat's avatar
Simon Morlat committed
142 143 144

	switch(t->base.state){
		case BELLE_SIP_TRANSACTION_TRYING: /*NON INVITE*/
145 146 147
			belle_sip_sender_task_send(t->base.stask,NULL);
			t->base.interval=MIN(t->base.interval*2,tc->T2);
			belle_sip_source_set_timeout(t->base.timer,t->base.interval);
Simon Morlat's avatar
Simon Morlat committed
148 149
		break;
		case BELLE_SIP_TRANSACTION_CALLING: /*INVITES*/
150 151 152
			belle_sip_sender_task_send(t->base.stask,NULL);
			t->base.interval=t->base.interval*2;
			belle_sip_source_set_timeout(t->base.timer,t->base.interval);
Simon Morlat's avatar
Simon Morlat committed
153 154 155
		break;
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			if (!t->base.is_invite){
156 157 158
				belle_sip_sender_task_send(t->base.stask,NULL);
				t->base.interval=tc->T2;
				belle_sip_source_set_timeout(t->base.timer,t->base.interval);
Simon Morlat's avatar
Simon Morlat committed
159 160 161 162 163 164 165 166
			}
		break;
		case BELLE_SIP_TRANSACTION_COMPLETED:
			belle_sip_transaction_terminate((belle_sip_transaction_t*)t);
			return BELLE_SIP_STOP;
		break;
		default:
			belle_sip_error("Unexpected transaction state %i while in timer callback",t->base.state);
Simon Morlat's avatar
Simon Morlat committed
167 168
	}
	if (belle_sip_time_ms()>=t->timer_F){
Simon Morlat's avatar
Simon Morlat committed
169 170 171 172 173 174
		/*report the timeout */
		belle_sip_timeout_event_t ev;
		ev.source=t->base.provider;
		ev.transaction=&t->base;
		ev.is_server_transaction=FALSE;
		BELLE_SIP_PROVIDER_INVOKE_LISTENERS(t->base.provider,process_timeout,&ev);
Simon Morlat's avatar
Simon Morlat committed
175 176 177
		belle_sip_transaction_terminate((belle_sip_transaction_t*)t);
		return BELLE_SIP_STOP;
	}
178
	return BELLE_SIP_CONTINUE;
179 180
}

181 182 183 184
static void client_transaction_cb(belle_sip_sender_task_t *task, void *data, int retcode){
	belle_sip_client_transaction_t *t=(belle_sip_client_transaction_t*)data;
	const belle_sip_timer_config_t *tc=belle_sip_stack_get_timer_config (belle_sip_provider_get_sip_stack (t->base.provider));
	if (retcode==0){
Simon Morlat's avatar
Simon Morlat committed
185
		t->base.is_reliable=belle_sip_sender_task_is_reliable(task);
Simon Morlat's avatar
Simon Morlat committed
186 187 188 189 190
		if (t->base.is_invite){
			t->base.state=BELLE_SIP_TRANSACTION_CALLING;
		}else{
			t->base.state=BELLE_SIP_TRANSACTION_TRYING;
		}
Simon Morlat's avatar
Simon Morlat committed
191 192 193
		t->base.start_time=belle_sip_time_ms();
		t->timer_F=t->base.start_time+(tc->T1*64);
		if (!t->base.is_reliable){
194 195
			t->base.interval=tc->T1;
			t->base.timer=transaction_create_timer(&t->base,on_client_transaction_timer,tc->T1);
Simon Morlat's avatar
Simon Morlat committed
196
		}else{
197
			t->base.timer=transaction_create_timer(&t->base,on_client_transaction_timer,tc->T1*64);
Simon Morlat's avatar
Simon Morlat committed
198
		}
199
	}else{
Simon Morlat's avatar
Simon Morlat committed
200 201
		/* transport layer error*/
		belle_sip_transaction_terminate(&t->base);
202
	}
203 204
}

205 206

void belle_sip_client_transaction_send_request(belle_sip_client_transaction_t *t){
207 208
	t->base.stask=belle_sip_sender_task_new(t->base.provider,client_transaction_cb,t);
	belle_sip_sender_task_send(t->base.stask,BELLE_SIP_MESSAGE(t->base.request));
Simon Morlat's avatar
Simon Morlat committed
209 210
}

Simon Morlat's avatar
Simon Morlat committed
211 212 213 214 215 216 217 218 219 220 221
static void notify_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
	belle_sip_provider_t *prov=t->base.provider;
	belle_sip_response_event_t ev;
	ev.source=prov;
	ev.client_transaction=t;
	ev.dialog=NULL;	/*TODO: FIND IT */
	ev.response=resp;
	BELLE_SIP_PROVIDER_INVOKE_LISTENERS(prov,process_response_event,&ev);
}

static void handle_invite_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
Simon Morlat's avatar
Simon Morlat committed
222 223 224 225
	int code=belle_sip_response_get_status_code(resp);
	
	if (code>=100 && code<200){
		switch(t->base.state){
Simon Morlat's avatar
Simon Morlat committed
226 227 228
			case BELLE_SIP_TRANSACTION_CALLING:
				if (!t->base.is_reliable){
					/* we must stop retransmissions, then program the timer B/F only*/
229
					belle_sip_source_set_timeout(t->base.timer,t->timer_F-belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
				}
				t->base.state=BELLE_SIP_TRANSACTION_PROCEEDING;
			case BELLE_SIP_TRANSACTION_PROCEEDING:
				if (t->base.prov_response!=NULL){
					belle_sip_object_unref(t->base.prov_response);
				}
				t->base.prov_response=(belle_sip_response_t*)belle_sip_object_ref(resp);
				notify_response(t,resp);
			break;
			default:
				belle_sip_warning("Unexpected provisional response while transaction in state %i",t->base.state);
		}
	}else if (code>=300){
		switch(t->base.state){
			case BELLE_SIP_TRANSACTION_CALLING:
			case BELLE_SIP_TRANSACTION_PROCEEDING:
				t->base.state=BELLE_SIP_TRANSACTION_COMPLETED;
				t->base.final_response=(belle_sip_response_t*)belle_sip_object_ref(resp);
				notify_response(t,resp);
				/*start timer D */
250
				belle_sip_source_set_timeout(t->base.timer,32000);
Simon Morlat's avatar
Simon Morlat committed
251 252 253 254 255 256 257 258
			break;
			default:
				belle_sip_warning("Unexpected final response while transaction in state %i",t->base.state);
		}
	}else if (code>=200){
		switch(t->base.state){
			case BELLE_SIP_TRANSACTION_CALLING:
			case BELLE_SIP_TRANSACTION_PROCEEDING:
259
				notify_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
				belle_sip_transaction_terminate(&t->base);
			break;
			default:
				belle_sip_warning("Unexpected final response while transaction in state %i",t->base.state);
		}
	}
}

static void handle_non_invite_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
	int code=belle_sip_response_get_status_code(resp);
	const belle_sip_timer_config_t *tc=belle_sip_stack_get_timer_config (belle_sip_provider_get_sip_stack (t->base.provider));
	
	if (code>=100 && code<200){
		switch(t->base.state){
			case BELLE_SIP_TRANSACTION_CALLING:
				if (!t->base.is_reliable){
					/* we must stop retransmissions, then program the timer B/F only*/
277
					belle_sip_source_set_timeout(t->base.timer,t->timer_F-belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
278
				}
Simon Morlat's avatar
Simon Morlat committed
279 280 281 282 283 284 285
			case BELLE_SIP_TRANSACTION_TRYING:
			case BELLE_SIP_TRANSACTION_PROCEEDING:
				t->base.state=BELLE_SIP_TRANSACTION_PROCEEDING;
				if (t->base.prov_response!=NULL){
					belle_sip_object_unref(t->base.prov_response);
				}
				t->base.prov_response=(belle_sip_response_t*)belle_sip_object_ref(resp);
Simon Morlat's avatar
Simon Morlat committed
286
				notify_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
287 288 289 290 291 292 293 294 295 296
			break;
			default:
				belle_sip_warning("Unexpected provisional response while transaction in state %i",t->base.state);
		}
	}else if (code>=200){
		switch(t->base.state){
			case BELLE_SIP_TRANSACTION_TRYING:
			case BELLE_SIP_TRANSACTION_PROCEEDING:
				t->base.state=BELLE_SIP_TRANSACTION_COMPLETED;
				t->base.final_response=(belle_sip_response_t*)belle_sip_object_ref(resp);
Simon Morlat's avatar
Simon Morlat committed
297
				notify_response(t,resp);
298
				belle_sip_source_set_timeout(t->base.timer,tc->T4);
Simon Morlat's avatar
Simon Morlat committed
299 300 301 302 303 304 305
			break;
			default:
				belle_sip_warning("Unexpected final response while transaction in state %i",t->base.state);
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
306 307 308 309 310 311
/*called by the transport layer when a response is received */
void belle_sip_client_transaction_add_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
	if (t->base.is_invite) handle_invite_response (t,resp);
	else handle_non_invite_response(t, resp);
}

Simon Morlat's avatar
Simon Morlat committed
312 313 314 315 316
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
	transaction_destroy((belle_sip_transaction_t*)t);
}


317
belle_sip_client_transaction_t * belle_sip_client_transaction_new(belle_sip_provider_t *prov, belle_sip_request_t *req){
Simon Morlat's avatar
Simon Morlat committed
318
	belle_sip_client_transaction_t *t=belle_sip_object_new(belle_sip_client_transaction_t,(belle_sip_object_destroy_t)client_transaction_destroy);
319
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
Simon Morlat's avatar
Simon Morlat committed
320 321
	if (strcmp(belle_sip_request_get_method(req),"INVITE")==0)
		t->base.is_invite=TRUE;
322 323 324 325 326
	return t;
}