ist.c 6.63 KB
Newer Older
1 2 3 4 5 6
/*
	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
7
    the Free Software Foundation, either version 2 of the License, or
8 9 10 11 12 13 14 15 16 17 18 19 20
    (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/>.
*/

/** 
 * INVITE server transaction implementation.
Simon Morlat's avatar
Simon Morlat committed
21
**/
22 23 24 25

#include "belle_sip_internal.h"

static void ist_on_terminate(belle_sip_ist_t *obj){
26
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
27
	/*timer pointers are set to NULL because they can be released later*/
28 29 30
	if (obj->timer_G){
		belle_sip_transaction_stop_timer(base,obj->timer_G);
		belle_sip_object_unref(obj->timer_G);
31
		obj->timer_G=NULL;
32 33 34 35
	}
	if (obj->timer_H){
		belle_sip_transaction_stop_timer(base,obj->timer_H);
		belle_sip_object_unref(obj->timer_H);
36
		obj->timer_H=NULL;
37 38 39 40
	}
	if (obj->timer_I){
		belle_sip_transaction_stop_timer(base,obj->timer_I);
		belle_sip_object_unref(obj->timer_I);
41
		obj->timer_I=NULL;
42
	}
43 44 45
	if (obj->timer_L){
		belle_sip_transaction_stop_timer(base,obj->timer_L);
		belle_sip_object_unref(obj->timer_L);
46
		obj->timer_L=NULL;
47
	}
48 49
}

50 51 52 53
static void ist_destroy(belle_sip_ist_t *obj){
	ist_on_terminate(obj);
}

54
/* Timer G: INVITE response retransmit interval */
55 56 57 58 59 60 61 62 63 64 65 66 67
static int ist_on_timer_G(belle_sip_ist_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	if (base->state==BELLE_SIP_TRANSACTION_COMPLETED){
		const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);
		int interval=belle_sip_source_get_timeout(obj->timer_G);
	
		belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)base->last_response);
		belle_sip_source_set_timeout(obj->timer_G,MIN(2*interval,cfg->T2));
		return BELLE_SIP_CONTINUE;
	}
	return BELLE_SIP_STOP;
}

68
/* Timer H: Wait time for ACK receipt */
69 70 71 72
static int ist_on_timer_H(belle_sip_ist_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	if (base->state==BELLE_SIP_TRANSACTION_COMPLETED){
		belle_sip_transaction_terminate(base);
73
		/*FIXME: no ACK was received, should report the failure */
74 75 76 77
	}
	return BELLE_SIP_STOP;
}

78
/* Timer I: Wait time for ACK retransmits */
79 80 81 82 83 84
static int ist_on_timer_I(belle_sip_ist_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	belle_sip_transaction_terminate(base);
	return BELLE_SIP_STOP;
}

85
/* Timer L: Wait time for accepted INVITE request retransmits */
86
static int ist_on_timer_L(belle_sip_ist_t *obj){
87
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	belle_sip_transaction_terminate(base);
	return BELLE_SIP_STOP;
}

int belle_sip_ist_process_ack(belle_sip_ist_t *obj, belle_sip_message_t *ack){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	int ret=-1;
	switch(base->state){
		case BELLE_SIP_TRANSACTION_COMPLETED:
			/*clear timer G*/
			if (obj->timer_G){
				belle_sip_transaction_stop_timer(base,obj->timer_G);
				belle_sip_object_unref(obj->timer_G);
				obj->timer_G=NULL;
			}
103
			belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_CONFIRMED);
104 105 106 107 108 109 110 111 112 113 114
			if (!belle_sip_channel_is_reliable(base->channel)){
				const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);
				obj->timer_I=belle_sip_timeout_source_new((belle_sip_source_func_t)ist_on_timer_I,obj,cfg->T4);
				belle_sip_transaction_start_timer(base,obj->timer_I);
			}else ist_on_timer_I(obj);
		break;
		case BELLE_SIP_TRANSACTION_ACCEPTED:
			ret=0; /*let the ACK be reported to TU */
		break;
		default:
		break;
115
	}
116
	return ret;
117 118
}

119 120 121
static int ist_send_new_response(belle_sip_ist_t *obj, belle_sip_response_t *resp){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	int code=belle_sip_response_get_status_code(resp);
122
	int ret=-1;
123 124
	switch(base->state){
		case BELLE_SIP_TRANSACTION_PROCEEDING:
125
			{
126
				const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);
127 128 129
				ret=0;
				belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)resp);
				if (code>=200 && code<300){
130
					belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_ACCEPTED);
131 132 133
					obj->timer_L=belle_sip_timeout_source_new((belle_sip_source_func_t)ist_on_timer_L,obj,64*cfg->T1);
					belle_sip_transaction_start_timer(base,obj->timer_L);
				}else if (code>=300){
134
					belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_COMPLETED);
135 136 137 138 139 140
					if (!belle_sip_channel_is_reliable(base->channel)){
						obj->timer_G=belle_sip_timeout_source_new((belle_sip_source_func_t)ist_on_timer_G,obj,cfg->T1);
						belle_sip_transaction_start_timer(base,obj->timer_G);
					}
					obj->timer_H=belle_sip_timeout_source_new((belle_sip_source_func_t)ist_on_timer_H,obj,64*cfg->T1);
					belle_sip_transaction_start_timer(base,obj->timer_H);
141 142
				}
			}
143
		break;
144 145 146 147
		case BELLE_SIP_TRANSACTION_ACCEPTED:
			if (code>=200 && code<300){
				ret=0; /*let the response go to transport layer*/
			}
148 149 150 151 152 153 154
		default:
		break;
	}
	return ret;
}

static void ist_on_request_retransmission(belle_sip_nist_t *obj){
155 156 157 158 159 160 161 162 163
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	switch(base->state){
		case BELLE_SIP_TRANSACTION_PROCEEDING:
		case BELLE_SIP_TRANSACTION_COMPLETED:
			belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)base->last_response);
		break;
		default:
		break;
	}
164 165
}

166 167 168

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_ist_t);

169
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_ist_t)
170 171 172
	{
		{
			{
173
				BELLE_SIP_VPTR_INIT(belle_sip_ist_t,belle_sip_server_transaction_t,TRUE),
174 175
				(belle_sip_object_destroy_t)ist_destroy,
				NULL,
176
				NULL,
177 178
				(belle_sip_object_on_first_ref_t) NULL,
				(belle_sip_object_on_last_ref_t) NULL,
179
				BELLE_SIP_DEFAULT_BUFSIZE_HINT
180 181 182
			},
			(void (*)(belle_sip_transaction_t *))ist_on_terminate
		},
183 184
		(int (*)(belle_sip_server_transaction_t*, belle_sip_response_t *))ist_send_new_response,
		(void (*)(belle_sip_server_transaction_t*))ist_on_request_retransmission,
185
	}
186
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
187 188 189 190


belle_sip_ist_t *belle_sip_ist_new(belle_sip_provider_t *prov, belle_sip_request_t *req){
	belle_sip_ist_t *obj=belle_sip_object_new(belle_sip_ist_t);
191
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
192 193

	belle_sip_server_transaction_init((belle_sip_server_transaction_t*)obj,prov,req);
194
	belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_PROCEEDING);
195 196
	return obj;
}