transaction.c 12 KB
Newer Older
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) 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"


22
#if 0
Simon Morlat's avatar
Simon Morlat committed
23
static belle_sip_source_t * transaction_create_timer(belle_sip_transaction_t *t, belle_sip_source_func_t func, unsigned int time_ms){
24
	belle_sip_stack_t *stack=belle_sip_provider_get_sip_stack(t->provider);
Simon Morlat's avatar
Simon Morlat committed
25 26 27
	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;
28
}
29
#endif
30 31

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

37 38 39 40 41 42 43 44
static void belle_sip_transaction_init(belle_sip_transaction_t *t, belle_sip_provider_t *prov, belle_sip_request_t *req){
	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
45 46
	if (t->prov_response) belle_sip_object_unref(t->prov_response);
	if (t->final_response) belle_sip_object_unref(t->final_response);
47
}
48

jehan's avatar
jehan committed
49
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_transaction_t,belle_sip_object_t,transaction_destroy,NULL,NULL);
Simon Morlat's avatar
Simon Morlat committed
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
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){
68 69 70 71 72 73
	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);
74 75 76 77 78 79
}

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

80
/*
81 82 83 84 85
 *
 *
 *	Server transaction
 *
 *
86 87 88 89 90 91
*/

struct belle_sip_server_transaction{
	belle_sip_transaction_t base;
};

92
#if 0
93 94 95
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){
96
		t->base.is_reliable=belle_sip_sender_task_is_reliable(st);
97 98 99 100 101
	}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);
	}
}
102
#endif
103

104
static void server_transaction_send_response(belle_sip_server_transaction_t *t, belle_sip_response_t *resp){
105 106
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/* called when a request retransmission is received for that transaction:*/
void belle_sip_server_transaction_retransmit(belle_sip_server_transaction_t *t){
	if (t->base.final_response!=NULL){
		server_transaction_send_response (t,t->base.final_response);
	}else if (t->base.prov_response!=NULL){
		server_transaction_send_response (t,t->base.prov_response);
	}
}

void belle_sip_server_transaction_send_response(belle_sip_server_transaction_t *t, belle_sip_response_t *resp){
	int status_code=belle_sip_response_get_status_code(resp);

	server_transaction_send_response(t,resp);
	
	if (status_code<200){
		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);
		t->base.state=BELLE_SIP_TRANSACTION_PROCEEDING;
	}else if (status_code<300){
		t->base.state=BELLE_SIP_TRANSACTION_TERMINATED;
		belle_sip_transaction_terminate((belle_sip_transaction_t*)t);
	}else{
		t->base.state=BELLE_SIP_TRANSACTION_COMPLETED;
	}
	
}

136 137 138
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
}

jehan's avatar
jehan committed
139
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_server_transaction_t,belle_sip_transaction_t,server_transaction_destroy,NULL,NULL);
Simon Morlat's avatar
Simon Morlat committed
140

141
belle_sip_server_transaction_t * belle_sip_server_transaction_new(belle_sip_provider_t *prov,belle_sip_request_t *req){
Simon Morlat's avatar
Simon Morlat committed
142
	belle_sip_server_transaction_t *t=belle_sip_object_new(belle_sip_server_transaction_t);
143 144 145 146 147
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
	return t;
}

/*
148 149 150 151 152
 *
 *
 *	Client transaction
 *
 *
153 154 155 156
*/

struct belle_sip_client_transaction{
	belle_sip_transaction_t base;
Simon Morlat's avatar
Simon Morlat committed
157 158 159
	uint64_t timer_F;
	uint64_t timer_E;
	uint64_t timer_K;
160 161
};

162 163 164
belle_sip_request_t * belle_sip_client_transaction_create_cancel(belle_sip_client_transaction_t *t){
	return NULL;
}
165
#if 0
166
static int on_client_transaction_timer(void *data, unsigned int revents){
Simon Morlat's avatar
Simon Morlat committed
167 168
	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
169 170 171

	switch(t->base.state){
		case BELLE_SIP_TRANSACTION_TRYING: /*NON INVITE*/
172
			//belle_sip_sender_task_send(t->base.stask,NULL);
173 174
			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
175 176
		break;
		case BELLE_SIP_TRANSACTION_CALLING: /*INVITES*/
177
			//belle_sip_sender_task_send(t->base.stask,NULL);
178 179
			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
180 181 182
		break;
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			if (!t->base.is_invite){
183
				//belle_sip_sender_task_send(t->base.stask,NULL);
184 185
				t->base.interval=tc->T2;
				belle_sip_source_set_timeout(t->base.timer,t->base.interval);
Simon Morlat's avatar
Simon Morlat committed
186 187 188 189 190 191 192 193
			}
		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
194 195
	}
	if (belle_sip_time_ms()>=t->timer_F){
Simon Morlat's avatar
Simon Morlat committed
196 197 198 199 200 201
		/*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
202 203 204
		belle_sip_transaction_terminate((belle_sip_transaction_t*)t);
		return BELLE_SIP_STOP;
	}
205
	return BELLE_SIP_CONTINUE;
206 207
}

208 209 210 211
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){
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
		if (t->base.state==BELLE_SIP_TRANSACTION_INIT){
			t->base.is_reliable=belle_sip_sender_task_is_reliable(task);
			if (t->base.is_invite){
				t->base.state=BELLE_SIP_TRANSACTION_CALLING;
			}else{
				t->base.state=BELLE_SIP_TRANSACTION_TRYING;
			}
			t->base.start_time=belle_sip_time_ms();
			t->timer_F=t->base.start_time+(tc->T1*64);
			if (!t->base.is_reliable){
				t->base.interval=tc->T1;
				t->base.timer=transaction_create_timer(&t->base,on_client_transaction_timer,tc->T1);
			}else{
				t->base.timer=transaction_create_timer(&t->base,on_client_transaction_timer,tc->T1*64);
			}
Simon Morlat's avatar
Simon Morlat committed
227
		}
228
	}else{
Simon Morlat's avatar
Simon Morlat committed
229 230
		/* transport layer error*/
		belle_sip_transaction_terminate(&t->base);
231
	}
232
}
233
#endif
234 235

void belle_sip_client_transaction_send_request(belle_sip_client_transaction_t *t){
236

Simon Morlat's avatar
Simon Morlat committed
237 238
}

Simon Morlat's avatar
Simon Morlat committed
239 240 241 242 243 244 245 246 247 248 249
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
250 251 252 253
	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
254 255 256
			case BELLE_SIP_TRANSACTION_CALLING:
				if (!t->base.is_reliable){
					/* we must stop retransmissions, then program the timer B/F only*/
257
					belle_sip_source_set_timeout(t->base.timer,t->timer_F-belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
				}
				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 */
278
				belle_sip_source_set_timeout(t->base.timer,32000);
Simon Morlat's avatar
Simon Morlat committed
279 280 281 282 283 284 285 286
			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:
287
				notify_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
				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*/
305
					belle_sip_source_set_timeout(t->base.timer,t->timer_F-belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
306
				}
Simon Morlat's avatar
Simon Morlat committed
307 308 309 310 311 312 313
			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
314
				notify_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
315 316 317 318 319 320 321 322 323 324
			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
325
				notify_response(t,resp);
326
				belle_sip_source_set_timeout(t->base.timer,tc->T4);
Simon Morlat's avatar
Simon Morlat committed
327 328 329 330 331 332 333
			break;
			default:
				belle_sip_warning("Unexpected final response while transaction in state %i",t->base.state);
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
334 335 336 337 338 339
/*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
340 341 342
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
}

jehan's avatar
jehan committed
343
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_client_transaction_t, belle_sip_transaction_t,client_transaction_destroy,NULL,NULL);
Simon Morlat's avatar
Simon Morlat committed
344

345
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
346
	belle_sip_client_transaction_t *t=belle_sip_object_new(belle_sip_client_transaction_t);
347
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
Simon Morlat's avatar
Simon Morlat committed
348
	if (req && strcmp(belle_sip_request_get_method(req),"INVITE")==0)
Simon Morlat's avatar
Simon Morlat committed
349
		t->base.is_invite=TRUE;
350 351 352 353 354
	return t;
}