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
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
46 47
	if (t->prov_response) belle_sip_object_unref(t->prov_response);
	if (t->final_response) belle_sip_object_unref(t->final_response);
48 49
	if (t->stask) belle_sip_object_unref(t->stask);
}
50

Simon Morlat's avatar
Simon Morlat committed
51 52
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_transaction_t,belle_sip_object_t,transaction_destroy,NULL);

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
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
}

Simon Morlat's avatar
Simon Morlat committed
113 114
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_server_transaction_t,belle_sip_transaction_t,server_transaction_destroy,NULL);

115
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
116
	belle_sip_server_transaction_t *t=belle_sip_object_new(belle_sip_server_transaction_t);
117 118 119 120 121
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
	return t;
}

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

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

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

140
static int on_client_transaction_timer(void *data, unsigned int revents){
Simon Morlat's avatar
Simon Morlat committed
141 142
	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
143 144 145

	switch(t->base.state){
		case BELLE_SIP_TRANSACTION_TRYING: /*NON INVITE*/
146 147 148
			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
149 150
		break;
		case BELLE_SIP_TRANSACTION_CALLING: /*INVITES*/
151 152 153
			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
154 155 156
		break;
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			if (!t->base.is_invite){
157 158 159
				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
160 161 162 163 164 165 166 167
			}
		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
168 169
	}
	if (belle_sip_time_ms()>=t->timer_F){
Simon Morlat's avatar
Simon Morlat committed
170 171 172 173 174 175
		/*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
176 177 178
		belle_sip_transaction_terminate((belle_sip_transaction_t*)t);
		return BELLE_SIP_STOP;
	}
179
	return BELLE_SIP_CONTINUE;
180 181
}

182 183 184 185
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
186
		t->base.is_reliable=belle_sip_sender_task_is_reliable(task);
Simon Morlat's avatar
Simon Morlat committed
187 188 189 190 191
		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
192 193 194
		t->base.start_time=belle_sip_time_ms();
		t->timer_F=t->base.start_time+(tc->T1*64);
		if (!t->base.is_reliable){
195 196
			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
197
		}else{
198
			t->base.timer=transaction_create_timer(&t->base,on_client_transaction_timer,tc->T1*64);
Simon Morlat's avatar
Simon Morlat committed
199
		}
200
	}else{
Simon Morlat's avatar
Simon Morlat committed
201 202
		/* transport layer error*/
		belle_sip_transaction_terminate(&t->base);
203
	}
204 205
}

206 207

void belle_sip_client_transaction_send_request(belle_sip_client_transaction_t *t){
208 209
	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
210 211
}

Simon Morlat's avatar
Simon Morlat committed
212 213 214 215 216 217 218 219 220 221 222
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
223 224 225 226
	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
227 228 229
			case BELLE_SIP_TRANSACTION_CALLING:
				if (!t->base.is_reliable){
					/* we must stop retransmissions, then program the timer B/F only*/
230
					belle_sip_source_set_timeout(t->base.timer,t->timer_F-belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
				}
				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 */
251
				belle_sip_source_set_timeout(t->base.timer,32000);
Simon Morlat's avatar
Simon Morlat committed
252 253 254 255 256 257 258 259
			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:
260
				notify_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
				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*/
278
					belle_sip_source_set_timeout(t->base.timer,t->timer_F-belle_sip_time_ms());
Simon Morlat's avatar
Simon Morlat committed
279
				}
Simon Morlat's avatar
Simon Morlat committed
280 281 282 283 284 285 286
			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
287
				notify_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
288 289 290 291 292 293 294 295 296 297
			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
298
				notify_response(t,resp);
299
				belle_sip_source_set_timeout(t->base.timer,tc->T4);
Simon Morlat's avatar
Simon Morlat committed
300 301 302 303 304 305 306
			break;
			default:
				belle_sip_warning("Unexpected final response while transaction in state %i",t->base.state);
		}
	}
}

Simon Morlat's avatar
Simon Morlat committed
307 308 309 310 311 312
/*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
313 314 315
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
}

Simon Morlat's avatar
Simon Morlat committed
316
BELLE_SIP_INSTANCIATE_VPTR(belle_sip_client_transaction_t, belle_sip_transaction_t,client_transaction_destroy,NULL);
Simon Morlat's avatar
Simon Morlat committed
317

318
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
319
	belle_sip_client_transaction_t *t=belle_sip_object_new(belle_sip_client_transaction_t);
320
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
Simon Morlat's avatar
Simon Morlat committed
321
	if (req && strcmp(belle_sip_request_get_method(req),"INVITE")==0)
Simon Morlat's avatar
Simon Morlat committed
322
		t->base.is_invite=TRUE;
323 324 325 326 327
	return t;
}