File: t_cancel.c

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (226 lines) | stat: -rw-r--r-- 6,168 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * $Id: t_cancel.c,v 1.4 2006/04/17 14:39:48 bogdan_iancu Exp $
 *
 * Copyright (C) 2001-2003 FhG Fokus
 *
 * This file is part of openser, a free SIP server.
 *
 * openser 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
 *
 * openser 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, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * History:
 * ----------
 * 2003-04-14  checking if a reply sent before cancel is initiated
 *             moved here (jiri)
 * 2004-02-11  FIFO/CANCEL + alignments (hash=f(callid,cseq)) (uli+jiri)
 * 2004-02-13  timer_link.payload removed (bogdan)
 */

#include <stdio.h> /* for FILE* in fifo_uac_cancel */

#include "t_funcs.h"
#include "../../dprint.h"
#include "../../ut.h"
#include "t_reply.h"
#include "t_cancel.h"
#include "t_msgbuilder.h"
#include "t_lookup.h" /* for t_lookup_callid in fifo_uac_cancel */
#include "../../fifo_server.h" /* for read_line() and fifo_reply() */
#include "../../unixsock_server.h"


/* determine which branches should be canceled; do it
   only from within REPLY_LOCK, otherwise collisions
   could occur (e.g., two 200 for two branches processed
   by two processes might concurrently try to generate
   a CANCEL for the third branch, resulting in race conditions
   during writing to cancel buffer
*/


void which_cancel( struct cell *t, branch_bm_t *cancel_bm )
{
	int i;

	for( i=t->first_branch ; i<t->nr_of_outgoings ; i++ ) {
		if (should_cancel_branch(t, i)) 
			*cancel_bm |= 1<<i ;

	}
}


/* cancel branches scheduled for deletion */
void cancel_uacs( struct cell *t, branch_bm_t cancel_bm )
{
	int i;

	/* cancel pending client transactions, if any */
	for( i=0 ; i<t->nr_of_outgoings ; i++ ) 
		if (cancel_bm & (1<<i))
			cancel_branch(t, i);
}


void cancel_branch( struct cell *t, int branch )
{
	char *cancel;
	unsigned int len;
	struct retr_buf *crb, *irb;

	crb=&t->uac[branch].local_cancel;
	irb=&t->uac[branch].request;

#	ifdef EXTRA_DEBUG
	if (crb->buffer.s!=0 && crb->buffer.s!=BUSY_BUFFER) {
		LOG(L_CRIT, "ERROR: attempt to rewrite cancel buffer\n");
		abort();
	}
#	endif

	if (t->uac[branch].last_received<100) {
		DBG("DEBUG: cancel_branch: no response ever received: "
		    "giving up on cancel\n");
		return;
	}

	cancel=build_cancel(t, branch, &len);
	if (!cancel) {
		LOG(L_ERR, "ERROR: attempt to build a CANCEL failed\n");
		return;
	}
	/* install cancel now */
	crb->buffer.s=cancel;
	crb->buffer.len=len;
	crb->dst=irb->dst;
	crb->branch=branch;
	/* label it as cancel so that FR timer can better now how 
	 * to deal with it */
	crb->activ_type=TYPE_LOCAL_CANCEL;

	if ( has_tran_tmcbs( t, TMCB_REQUEST_BUILT) ) {
		set_extra_tmcb_params( &crb->buffer, &crb->dst);
		run_trans_callbacks( TMCB_REQUEST_BUILT, t, t->uas.request,0,
			-t->uas.request->REQ_METHOD);
	}

	DBG("DEBUG: cancel_branch: sending cancel...\n");
	SEND_BUFFER( crb );

	/*sets and starts the FINAL RESPONSE timer */
	start_retr( crb );
}


char *build_cancel(struct cell *Trans,unsigned int branch,
	unsigned int *len )
{
	return build_local( Trans, branch, len,
		CANCEL, CANCEL_LEN, &Trans->to );
}

/* fifo command to cancel a pending call (Uli)
  Syntax:

  ":uac_cancel:[response file]\n
  callid\n
 cseq\n
  \n"
 */

int fifo_uac_cancel( FILE* stream, char *response_file )
{
	struct cell *trans;
	static char cseq[128], callid[128];

	str cseq_s;   /* cseq */
	str callid_s; /* callid */

	cseq_s.s=cseq;
	callid_s.s=callid;

	DBG("DEBUG: fifo_uac_cancel: ############### begin ##############\n");

	/* first param callid read */
	if (!read_line(callid_s.s, 128, stream, &callid_s.len)||callid_s.len==0) {
		LOG(L_ERR, "ERROR: fifo_uac_cancel: callid expected\n");
		fifo_reply(response_file, "400 fifo_uac_cancel: callid expected");
		return -1;
	}
	callid_s.s[callid_s.len]='\0';
	DBG("DEBUG: fifo_uac_cancel: callid=\"%.*s\"\n",callid_s.len, callid_s.s);

	/* second param cseq read */
	if (!read_line(cseq_s.s, 128, stream, &cseq_s.len)||cseq_s.len==0) {
		LOG(L_ERR, "ERROR: fifo_uac_cancel: cseq expected\n");
		fifo_reply(response_file, "400 fifo_uac_cancel: cseq expected");
		return -1;
	}
	cseq_s.s[cseq_s.len]='\0';
	DBG("DEBUG: fifo_uac_cancel: cseq=\"%.*s\"\n",cseq_s.len, cseq_s.s);

	if( t_lookup_callid(&trans, callid_s, cseq_s) < 0 ) {
		LOG(L_ERR,"ERROR: fifo_uac_cancel: lookup failed\n");
		fifo_reply(response_file, "481 fifo_uac_cancel: no such transaction");
		return -1;
	}
	/* tell tm to cancel the call */
	DBG("DEBUG: fifo_uac_cancel: now calling cancel_uacs\n");
	(*cancel_uacs)(trans,~0);

	/* t_lookup_callid REF`d the transaction for us, we must UNREF here! */
	UNREF(trans);

	fifo_reply(response_file, "200 fifo_uac_cancel succeeded\n");
	DBG("DEBUG: fifo_uac_cancel: ################ end ##############\n");
	return 1;
}


int unixsock_uac_cancel(str* msg)
{
	struct cell *trans;
	str cseq, callid;

	     /* first param callid read */
	if (unixsock_read_line(&callid, msg) != 0) {
		unixsock_reply_asciiz("400 Call-ID Expected\n");
		unixsock_reply_send();
		return -1;
	}

	     /* second param cseq read */
	if (unixsock_read_line(&cseq, msg) != 0) {
		unixsock_reply_asciiz("400 CSeq Expected\n");
		unixsock_reply_send();
		return -1;
	}

	if (t_lookup_callid(&trans, callid, cseq) < 0) {
		LOG(L_ERR, "unixsock_uac_cancel: Lookup failed\n");
		unixsock_reply_asciiz("481 uac_cancel: No such transaction\n");
		unixsock_reply_send();
		return 1;
	}

	     /* tell tm to cancel the call */
	(*cancel_uacs)(trans, ~0);

	     /* t_lookup_callid REF`d the transaction for us, we must UNREF here! */
	UNREF(trans);

	unixsock_reply_asciiz("200 uac_cancel succeeded\n");
	unixsock_reply_send();
	return 0;
}