nict.c 6.78 KB
Newer Older
Simon Morlat's avatar
Simon Morlat committed
1 2
/*
	belle-sip - SIP (RFC3261) library.
3
	Copyright (C) 2010-2018  Belledonne Communications SARL
Simon Morlat's avatar
Simon Morlat committed
4

5 6 7 8
	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 2 of the License, or
	(at your option) any later version.
Simon Morlat's avatar
Simon Morlat committed
9

10 11 12 13
	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.
Simon Morlat's avatar
Simon Morlat committed
14

15 16
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
Simon Morlat's avatar
Simon Morlat committed
17 18
*/

19
/**
20 21 22
 * non-INVITE client transaction implementation.
**/

Simon Morlat's avatar
Simon Morlat committed
23 24 25 26 27 28 29
#include "belle_sip_internal.h"

static int nict_on_timer_K(belle_sip_nict_t *obj){
	belle_sip_transaction_terminate((belle_sip_transaction_t*)obj);
	return BELLE_SIP_STOP;
}

30
static void nict_set_completed(belle_sip_nict_t *obj, belle_sip_response_t *resp){
Simon Morlat's avatar
Simon Morlat committed
31 32
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);
33
	belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_COMPLETED);
Simon Morlat's avatar
Simon Morlat committed
34
	if (obj->timer_K) belle_sip_fatal("Should never happen.");
35 36 37 38

	belle_sip_client_transaction_notify_response((belle_sip_client_transaction_t*)obj,resp);

	if (!belle_sip_channel_is_reliable(base->channel)){
39 40 41 42 43 44
		/* FIXME: Temporary workaround for -Wcast-function-type. */
		#if __GNUC__ >= 8
			_Pragma("GCC diagnostic push")
			_Pragma("GCC diagnostic ignored \"-Wcast-function-type\"")
		#endif // if __GNUC__ >= 8

45
		obj->timer_K=belle_sip_timeout_source_new((belle_sip_source_func_t)nict_on_timer_K,obj,cfg->T4);
46 47 48 49 50

		#if __GNUC__ >= 8
			_Pragma("GCC diagnostic pop")
		#endif // if __GNUC__ >= 8

51
		belle_sip_object_set_name((belle_sip_object_t*)obj->timer_K,"timer_K");
52 53
		belle_sip_transaction_start_timer(base,obj->timer_K);
	}else belle_sip_transaction_terminate(base);
Simon Morlat's avatar
Simon Morlat committed
54 55
}

56
static void nict_on_response(belle_sip_nict_t *obj, belle_sip_response_t *resp){
Simon Morlat's avatar
Simon Morlat committed
57 58
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	int code=belle_sip_response_get_status_code(resp);
59

Simon Morlat's avatar
Simon Morlat committed
60 61 62
	switch(base->state){
		case BELLE_SIP_TRANSACTION_TRYING:
			if (code<200){
63
				belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_PROCEEDING);
64
				belle_sip_client_transaction_notify_response((belle_sip_client_transaction_t*)obj,resp);
Simon Morlat's avatar
Simon Morlat committed
65 66
			}
			else {
67
				nict_set_completed(obj,resp);
Simon Morlat's avatar
Simon Morlat committed
68 69 70 71
			}
		break;
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			if (code>=200){
72
				nict_set_completed(obj,resp);
Simon Morlat's avatar
Simon Morlat committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
			}
		break;
		default:
		break;
	}
}

static void nict_on_terminate(belle_sip_nict_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	if (obj->timer_F){
		belle_sip_transaction_stop_timer(base,obj->timer_F);
		belle_sip_object_unref(obj->timer_F);
		obj->timer_F=NULL;
	}
	if (obj->timer_E){
		belle_sip_transaction_stop_timer(base,obj->timer_E);
		belle_sip_object_unref(obj->timer_E);
		obj->timer_E=NULL;
	}
	if (obj->timer_K){
		belle_sip_transaction_stop_timer(base,obj->timer_K);
		belle_sip_object_unref(obj->timer_K);
		obj->timer_K=NULL;
	}
}

static int nict_on_timer_F(belle_sip_nict_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	switch (base->state){
		case BELLE_SIP_TRANSACTION_TRYING:
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			belle_sip_transaction_notify_timeout(base);
		break;
		default:
		break;
	}
	return BELLE_SIP_STOP;
}

static int nict_on_timer_E(belle_sip_nict_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);

	switch(base->state){
		case BELLE_SIP_TRANSACTION_TRYING:
		{
			/*reset the timer */
			unsigned int prev_timeout=belle_sip_source_get_timeout(obj->timer_E);
121
			belle_sip_source_set_timeout(obj->timer_E,MIN(2*prev_timeout,(unsigned int)cfg->T2));
122
			belle_sip_message("nict_on_timer_E: sending retransmission");
Simon Morlat's avatar
Simon Morlat committed
123 124 125 126 127
			belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)base->request);
		}
		break;
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			belle_sip_source_set_timeout(obj->timer_E,cfg->T2);
128
			belle_sip_message("nict_on_timer_E: sending retransmission");
Simon Morlat's avatar
Simon Morlat committed
129 130 131
			belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)base->request);
		break;
		default:
132 133
			/*if we are not in these cases, timer_E does nothing, so remove it*/
			return BELLE_SIP_STOP;
Simon Morlat's avatar
Simon Morlat committed
134 135 136 137 138
		break;
	}
	return BELLE_SIP_CONTINUE;
}

139 140 141 142 143 144 145 146 147
static void nict_stop_retransmissions(belle_sip_nict_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	if (obj->timer_E){
		belle_sip_transaction_stop_timer(base,obj->timer_E);
		belle_sip_object_unref(obj->timer_E);
		obj->timer_E=NULL;
	}
}

Simon Morlat's avatar
Simon Morlat committed
148 149 150
static void nict_send_request(belle_sip_nict_t *obj){
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)obj;
	const belle_sip_timer_config_t *cfg=belle_sip_transaction_get_timer_config(base);
151 152 153 154 155 156 157

	/* FIXME: Temporary workaround for -Wcast-function-type. */
	#if __GNUC__ >= 8
		_Pragma("GCC diagnostic push")
		_Pragma("GCC diagnostic ignored \"-Wcast-function-type\"")
	#endif // if __GNUC__ >= 8

158
	belle_sip_transaction_set_state(base,BELLE_SIP_TRANSACTION_TRYING);
Simon Morlat's avatar
Simon Morlat committed
159
	obj->timer_F=belle_sip_timeout_source_new((belle_sip_source_func_t)nict_on_timer_F,obj,cfg->T1*64);
160
	belle_sip_object_set_name((belle_sip_object_t*)obj->timer_F,"timer_F");
Simon Morlat's avatar
Simon Morlat committed
161
	belle_sip_transaction_start_timer(base,obj->timer_F);
162

Simon Morlat's avatar
Simon Morlat committed
163 164
	if (!belle_sip_channel_is_reliable(base->channel)){
		obj->timer_E=belle_sip_timeout_source_new((belle_sip_source_func_t)nict_on_timer_E,obj,cfg->T1);
165
		belle_sip_object_set_name((belle_sip_object_t*)obj->timer_E,"timer_E");
Simon Morlat's avatar
Simon Morlat committed
166 167
		belle_sip_transaction_start_timer(base,obj->timer_E);
	}
168 169 170 171 172

	#if __GNUC__ >= 8
		_Pragma("GCC diagnostic pop")
	#endif // if __GNUC__ >= 8

Simon Morlat's avatar
Simon Morlat committed
173
	belle_sip_channel_queue_message(base->channel,(belle_sip_message_t*)base->request);
Simon Morlat's avatar
Simon Morlat committed
174 175
}

176 177 178 179
static void nict_destroy(belle_sip_nict_t *obj){
	nict_on_terminate(obj);
}

Simon Morlat's avatar
Simon Morlat committed
180 181
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_nict_t);

182
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_BEGIN(belle_sip_nict_t)
Simon Morlat's avatar
Simon Morlat committed
183 184 185
	{
		{
			{
186
				BELLE_SIP_VPTR_INIT(belle_sip_nict_t,belle_sip_client_transaction_t,TRUE),
Simon Morlat's avatar
Simon Morlat committed
187 188
				(belle_sip_object_destroy_t)nict_destroy,
				NULL,
189 190
				NULL,
				BELLE_SIP_DEFAULT_BUFSIZE_HINT
Simon Morlat's avatar
Simon Morlat committed
191 192 193 194
			},
			(void (*)(belle_sip_transaction_t *))nict_on_terminate
		},
		(void (*)(belle_sip_client_transaction_t*))nict_send_request,
195 196
		(void (*)(belle_sip_client_transaction_t*,belle_sip_response_t*))nict_on_response,
		(void (*)(belle_sip_client_transaction_t*))nict_stop_retransmissions
Simon Morlat's avatar
Simon Morlat committed
197
	}
198
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR_END
Simon Morlat's avatar
Simon Morlat committed
199 200 201 202 203 204 205


belle_sip_nict_t *belle_sip_nict_new(belle_sip_provider_t *prov, belle_sip_request_t *req){
	belle_sip_nict_t *obj=belle_sip_object_new(belle_sip_nict_t);
	belle_sip_client_transaction_init((belle_sip_client_transaction_t*)obj,prov,req);
	return obj;
}