transaction.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
	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"

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
const char *belle_sip_transaction_state_to_string(belle_sip_transaction_state_t state){
	switch(state){
		case BELLE_SIP_TRANSACTION_INIT:
			return "INIT";
		case BELLE_SIP_TRANSACTION_TRYING:
			return "TRYING";
		case BELLE_SIP_TRANSACTION_CALLING:
			return "CALLING";
		case BELLE_SIP_TRANSACTION_COMPLETED:
			return "COMPLETED";
		case BELLE_SIP_TRANSACTION_CONFIRMED:
			return "CONFIRMED";
		case BELLE_SIP_TRANSACTION_PROCEEDING:
			return "PROCEEDING";
		case BELLE_SIP_TRANSACTION_TERMINATED:
			return "TERMINATED";
	}
	belle_sip_fatal("Invalid transaction state.");
	return "INVALID";
}

42
static void belle_sip_transaction_init(belle_sip_transaction_t *t, belle_sip_provider_t *prov, belle_sip_request_t *req){
Simon Morlat's avatar
Simon Morlat committed
43
	t->request=(belle_sip_request_t*)belle_sip_object_ref(req);
44 45 46 47 48
	t->provider=prov;
}

static void transaction_destroy(belle_sip_transaction_t *t){
	if (t->request) belle_sip_object_unref(t->request);
49
	if (t->last_response) belle_sip_object_unref(t->last_response);
Simon Morlat's avatar
Simon Morlat committed
50
	if (t->channel) belle_sip_object_unref(t->channel);
51
}
52

53
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_transaction_t);
Simon Morlat's avatar
Simon Morlat committed
54 55

BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_transaction_t)={
Simon Morlat's avatar
Simon Morlat committed
56
	{
Simon Morlat's avatar
Simon Morlat committed
57 58 59 60
		BELLE_SIP_VPTR_INIT(belle_sip_transaction_t,belle_sip_object_t,FALSE),
		(belle_sip_object_destroy_t) transaction_destroy,
		NULL,/*no clone*/
		NULL,/*no marshall*/
Simon Morlat's avatar
Simon Morlat committed
61 62
	},
	NULL /*on_terminate*/
Simon Morlat's avatar
Simon Morlat committed
63
};
Simon Morlat's avatar
Simon Morlat committed
64

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
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){
82 83
	t->state=BELLE_SIP_TRANSACTION_TERMINATED;
	belle_sip_provider_set_transaction_terminated(t->provider,t);
Simon Morlat's avatar
Simon Morlat committed
84
	BELLE_SIP_OBJECT_VPTR(t,belle_sip_transaction_t)->on_terminate(t);
85 86 87 88 89 90
}

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

Simon Morlat's avatar
Simon Morlat committed
91 92 93 94 95 96 97 98
void belle_sip_transaction_notify_timeout(belle_sip_transaction_t *t){
	belle_sip_timeout_event_t ev;
	ev.source=t->provider;
	ev.transaction=t;
	ev.is_server_transaction=BELLE_SIP_OBJECT_IS_INSTANCE_OF(t,belle_sip_server_transaction_t);
	BELLE_SIP_PROVIDER_INVOKE_LISTENERS(t->provider,process_timeout,&ev);
}

99
/*
Simon Morlat's avatar
Simon Morlat committed
100 101
 * Server transaction
 */
102

103 104 105
static void server_transaction_destroy(belle_sip_server_transaction_t *t){
}

Simon Morlat's avatar
Simon Morlat committed
106

107
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sip_server_transaction_t);
Simon Morlat's avatar
Simon Morlat committed
108
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_server_transaction_t)={
Simon Morlat's avatar
Simon Morlat committed
109
	{
Simon Morlat's avatar
Simon Morlat committed
110 111 112 113 114
		{
			BELLE_SIP_VPTR_INIT(belle_sip_server_transaction_t,belle_sip_transaction_t,FALSE),
			(belle_sip_object_destroy_t) server_transaction_destroy,
			NULL,
			NULL
Simon Morlat's avatar
Simon Morlat committed
115 116 117
		},
		NULL
	}
Simon Morlat's avatar
Simon Morlat committed
118
};
Simon Morlat's avatar
Simon Morlat committed
119

120
void belle_sip_server_transaction_init(belle_sip_server_transaction_t *t, belle_sip_provider_t *prov,belle_sip_request_t *req){
121
	belle_sip_transaction_init((belle_sip_transaction_t*)t,prov,req);
122
	belle_sip_random_token(t->to_tag,sizeof(t->to_tag));
123 124
}

Simon Morlat's avatar
Simon Morlat committed
125
void belle_sip_server_transaction_send_response(belle_sip_server_transaction_t *t, belle_sip_response_t *resp){
126
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)t;
127
	belle_sip_header_to_t *to=(belle_sip_header_to_t*)belle_sip_message_get_header((belle_sip_message_t*)resp,"to");
128 129 130 131 132 133
	belle_sip_object_ref(resp);
	if (!base->last_response){
		belle_sip_hop_t hop;
		belle_sip_response_get_return_hop(resp,&hop);
		base->channel=belle_sip_provider_get_channel(base->provider,hop.host, hop.port, hop.transport);
		belle_sip_object_ref(base->channel);
134
		belle_sip_hop_free(&hop);
135
	}
136 137 138 139
	if (belle_sip_header_to_get_tag(to)==NULL && belle_sip_response_get_status_code(resp)!=100){
		//add a random to tag
		belle_sip_header_to_set_tag(to,t->to_tag);
	}
140 141 142 143 144 145 146
	if (BELLE_SIP_OBJECT_VPTR(t,belle_sip_server_transaction_t)->send_new_response(t,resp)==0){
		if (base->last_response)
			belle_sip_object_unref(base->last_response);
		base->last_response=resp;
	}
}

Simon Morlat's avatar
Simon Morlat committed
147 148 149
void belle_sip_server_transaction_on_request(belle_sip_server_transaction_t *t, belle_sip_request_t *req){
	const char *method=belle_sip_request_get_method(req);
	if (strcmp(method,"ACK")==0){
150 151 152 153 154 155 156
		/*this must be for an INVITE server transaction */
		if (BELLE_SIP_OBJECT_IS_INSTANCE_OF(t,belle_sip_ist_t)){
			belle_sip_ist_t *ist=(belle_sip_ist_t*)t;
			belle_sip_ist_process_ack(ist,(belle_sip_message_t*)req);
		}else{
			belle_sip_warning("ACK received for non-invite server transaction ?");
		}
Simon Morlat's avatar
Simon Morlat committed
157
	}else if (strcmp(method,"CANCEL")==0){
158 159 160 161 162 163 164 165
		/*just notify the application */
		belle_sip_request_event_t event;

		event.source=t->base.provider;
		event.server_transaction=t;
		event.dialog=NULL;
		event.request=req;
		BELLE_SIP_PROVIDER_INVOKE_LISTENERS(t->base.provider,process_request_event,&event);
Simon Morlat's avatar
Simon Morlat committed
166 167
	}else
		BELLE_SIP_OBJECT_VPTR(t,belle_sip_server_transaction_t)->on_request_retransmission(t);
Simon Morlat's avatar
Simon Morlat committed
168
}
169

Simon Morlat's avatar
Simon Morlat committed
170 171 172 173
/*
 * client transaction
 */

174

175

176
belle_sip_request_t * belle_sip_client_transaction_create_cancel(belle_sip_client_transaction_t *t){
177 178 179 180 181 182 183 184 185 186 187 188 189 190
	belle_sip_message_t *orig=(belle_sip_message_t*)t->base.request;
	belle_sip_request_t *req;
	const char *orig_method=belle_sip_request_get_method((belle_sip_request_t*)orig);
	if (strcmp(orig_method,"ACK")==0 || strcmp(orig_method,"INVITE")!=0){
		belle_sip_error("belle_sip_client_transaction_create_cancel() cannot be used for ACK or non-INVITE transactions.");
		return NULL;
	}
	if (t->base.state==BELLE_SIP_TRANSACTION_PROCEEDING){
		belle_sip_error("belle_sip_client_transaction_create_cancel() can only be used in state BELLE_SIP_TRANSACTION_PROCEEDING"
		               " but current transaction state is %s",belle_sip_transaction_state_to_string(t->base.state));
	}
	req=belle_sip_request_new();
	belle_sip_request_set_method(req,"CANCEL");
	belle_sip_request_set_uri(req,(belle_sip_uri_t*)belle_sip_object_clone((belle_sip_object_t*)belle_sip_request_get_uri((belle_sip_request_t*)orig)));
191 192 193 194 195
	belle_sip_util_copy_headers(orig,(belle_sip_message_t*)req,"via",FALSE);
	belle_sip_util_copy_headers(orig,(belle_sip_message_t*)req,"call-id",FALSE);
	belle_sip_util_copy_headers(orig,(belle_sip_message_t*)req,"from",FALSE);
	belle_sip_util_copy_headers(orig,(belle_sip_message_t*)req,"to",FALSE);
	belle_sip_util_copy_headers(orig,(belle_sip_message_t*)req,"route",TRUE);
196 197 198 199 200
	belle_sip_message_add_header((belle_sip_message_t*)req,
		(belle_sip_header_t*)belle_sip_header_cseq_create(
			belle_sip_header_cseq_get_seq_number((belle_sip_header_cseq_t*)belle_sip_message_get_header(orig,"cseq")),
		    "CANCEL"));
	return req;
201
}
Simon Morlat's avatar
Simon Morlat committed
202

203 204

void belle_sip_client_transaction_send_request(belle_sip_client_transaction_t *t){
Simon Morlat's avatar
Simon Morlat committed
205 206
	belle_sip_hop_t hop={0};
	belle_sip_channel_t *chan;
Simon Morlat's avatar
Simon Morlat committed
207
	belle_sip_provider_t *prov=t->base.provider;
Simon Morlat's avatar
Simon Morlat committed
208
	
Simon Morlat's avatar
Simon Morlat committed
209 210 211
	if (t->base.state!=BELLE_SIP_TRANSACTION_INIT){
		belle_sip_error("belle_sip_client_transaction_send_request: bad state.");
		return;
Simon Morlat's avatar
Simon Morlat committed
212
	}
Simon Morlat's avatar
Simon Morlat committed
213 214 215
	belle_sip_stack_get_next_hop(prov->stack,t->base.request,&hop);
	chan=belle_sip_provider_get_channel(prov,hop.host, hop.port, hop.transport);
	if (chan){
216
		belle_sip_provider_add_client_transaction(t->base.provider,t);
Simon Morlat's avatar
Simon Morlat committed
217
		belle_sip_object_ref(chan);
Simon Morlat's avatar
Simon Morlat committed
218 219
		belle_sip_channel_add_listener(chan,BELLE_SIP_CHANNEL_LISTENER(t));
		t->base.channel=chan;
Simon Morlat's avatar
Simon Morlat committed
220 221 222 223
		if (belle_sip_channel_get_state(chan)==BELLE_SIP_CHANNEL_INIT)
			belle_sip_channel_prepare(chan);
		if (belle_sip_channel_get_state(chan)!=BELLE_SIP_CHANNEL_READY){
			belle_sip_message("belle_sip_client_transaction_send_request(): waiting channel to be ready");
224 225
		} else {
			BELLE_SIP_OBJECT_VPTR(t,belle_sip_client_transaction_t)->send_request(t);
Simon Morlat's avatar
Simon Morlat committed
226
		}
Simon Morlat's avatar
Simon Morlat committed
227
	}else belle_sip_error("belle_sip_client_transaction_send_request(): no channel available");
228
	belle_sip_hop_free(&hop);
Simon Morlat's avatar
Simon Morlat committed
229 230
}

231
void belle_sip_client_transaction_notify_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
Simon Morlat's avatar
Simon Morlat committed
232
	belle_sip_transaction_t *base=(belle_sip_transaction_t*)t;
233 234
	belle_sip_response_event_t event;
		
235 236 237
	if (base->last_response)
		belle_sip_object_unref(base->last_response);
	base->last_response=(belle_sip_response_t*)belle_sip_object_ref(resp);
238 239 240 241 242 243 244 245 246 247 248

	event.source=base->provider;
	event.client_transaction=t;
	event.dialog=NULL;
	event.response=(belle_sip_response_t*)resp;
	BELLE_SIP_PROVIDER_INVOKE_LISTENERS(base->provider,process_response_event,&event);
}


void belle_sip_client_transaction_add_response(belle_sip_client_transaction_t *t, belle_sip_response_t *resp){
	BELLE_SIP_OBJECT_VPTR(t,belle_sip_client_transaction_t)->on_response(t,resp);
Simon Morlat's avatar
Simon Morlat committed
249 250
}

Simon Morlat's avatar
Simon Morlat committed
251
static void client_transaction_destroy(belle_sip_client_transaction_t *t ){
Simon Morlat's avatar
Simon Morlat committed
252 253
}

Simon Morlat's avatar
Simon Morlat committed
254 255
static void on_channel_state_changed(belle_sip_channel_listener_t *l, belle_sip_channel_t *chan, belle_sip_channel_state_t state){
	belle_sip_client_transaction_t *t=(belle_sip_client_transaction_t*)l;
Simon Morlat's avatar
Simon Morlat committed
256
	belle_sip_message("transaction on_channel_state_changed");
Simon Morlat's avatar
Simon Morlat committed
257 258 259 260 261 262 263 264
	switch(state){
		case BELLE_SIP_CHANNEL_READY:
			BELLE_SIP_OBJECT_VPTR(t,belle_sip_client_transaction_t)->send_request(t);
		break;
		default:
			/*ignored*/
		break;
	}
Simon Morlat's avatar
Simon Morlat committed
265 266
}

Simon Morlat's avatar
Simon Morlat committed
267 268 269 270 271 272 273 274 275
BELLE_SIP_IMPLEMENT_INTERFACE_BEGIN(belle_sip_client_transaction_t,belle_sip_channel_listener_t)
on_channel_state_changed,
NULL,
NULL
BELLE_SIP_IMPLEMENT_INTERFACE_END

BELLE_SIP_DECLARE_IMPLEMENTED_INTERFACES_1(belle_sip_client_transaction_t, belle_sip_channel_listener_t);
BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_client_transaction_t)={
	{
Simon Morlat's avatar
Simon Morlat committed
276 277 278 279 280 281
		{
			BELLE_SIP_VPTR_INIT(belle_sip_client_transaction_t,belle_sip_transaction_t,FALSE),
			(belle_sip_object_destroy_t)client_transaction_destroy,
			NULL,
			NULL
		},
Simon Morlat's avatar
Simon Morlat committed
282 283 284 285 286
		NULL
	},
	NULL,
	NULL
};
Simon Morlat's avatar
Simon Morlat committed
287

Simon Morlat's avatar
Simon Morlat committed
288
void belle_sip_client_transaction_init(belle_sip_client_transaction_t *obj, belle_sip_provider_t *prov, belle_sip_request_t *req){
Simon Morlat's avatar
Simon Morlat committed
289 290
	belle_sip_header_via_t *via=BELLE_SIP_HEADER_VIA(belle_sip_message_get_header((belle_sip_message_t*)req,"via"));
	char token[10];
291 292 293 294 295 296 297

	if (!via){
		belle_sip_fatal("belle_sip_client_transaction_init(): No via in request.");
	}
	
	if (strcmp(belle_sip_request_get_method(req),"CANCEL")!=0){
		obj->base.branch_id=belle_sip_strdup_printf(BELLE_SIP_BRANCH_MAGIC_COOKIE ".%s",belle_sip_random_token(token,sizeof(token)));
Simon Morlat's avatar
Simon Morlat committed
298 299
		belle_sip_header_via_set_branch(via,obj->base.branch_id);
	}else{
300
		obj->base.branch_id=belle_sip_strdup(belle_sip_header_via_get_branch(via));
Simon Morlat's avatar
Simon Morlat committed
301
	}
Simon Morlat's avatar
Simon Morlat committed
302
	belle_sip_transaction_init((belle_sip_transaction_t*)obj, prov,req);
303 304
}

305