ist.c 6.49 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
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;
}

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);
		/*FIXME: no ACK was received, should report the faillure */
	}
	return BELLE_SIP_STOP;
}

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;
}

82
static int ist_on_timer_L(belle_sip_ist_t *obj){
83
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	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;
			}
99
			belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_CONFIRMED);
100 101 102 103 104 105 106 107 108 109 110
			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;
111
	}
112
	return ret;
113 114
}

115 116 117
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);
118
	int ret=-1;
119 120
	switch(base->state){
		case BELLE_SIP_TRANSACTION_PROCEEDING:
121
			{
122
				const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);
123 124 125
				ret=0;
				belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)resp);
				if (code>=200 && code<300){
126
					belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_ACCEPTED);
127 128 129
					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){
130
					belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_COMPLETED);
131 132 133 134 135 136
					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);
137 138
				}
			}
139
		break;
140 141 142 143
		case BELLE_SIP_TRANSACTION_ACCEPTED:
			if (code>=200 && code<300){
				ret=0; /*let the response go to transport layer*/
			}
144 145 146 147 148 149 150
		default:
		break;
	}
	return ret;
}

static void ist_on_request_retransmission(belle_sip_nist_t *obj){
151 152 153 154 155 156 157 158 159
	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;
	}
160 161
}

162 163 164

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_ist_t);

165
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_ist_t)
166 167 168
	{
		{
			{
169
				BELLE_SIP_VPTR_INIT(belle_sip_ist_t,belle_sip_server_transaction_t,TRUE),
170 171 172 173 174 175
				(belle_sip_object_destroy_t)ist_destroy,
				NULL,
				NULL
			},
			(void (*)(belle_sip_transaction_t *))ist_on_terminate
		},
176 177
		(int (*)(belle_sip_server_transaction_t*, belle_sip_response_t *))ist_send_new_response,
		(void (*)(belle_sip_server_transaction_t*))ist_on_request_retransmission,
178
	}
179
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
180 181 182 183


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);
184 185
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	belle_sip_response_t *resp;
186 187

	belle_sip_server_transaction_init((belle_sip_server_transaction_t*)obj,prov,req);
188
	belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_PROCEEDING);
189 190
	resp=belle_sip_response_create_from_request(req,100);
	belle_sip_server_transaction_send_response((belle_sip_server_transaction_t*)obj,resp);
191 192
	return obj;
}