- replaced msg->src_ip w/ msg->rcv.src_ip (to be in sync w/ core)$
[sip-router] / modules / tm / t_fwd.c
1 /*
2  * $Id$
3  *
4  *
5  * Copyright (C) 2001-2003 Fhg Fokus
6  *
7  * This file is part of ser, a free SIP server.
8  *
9  * ser is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version
13  *
14  * For a license to use the ser software under conditions
15  * other than those described here, or to purchase support for this
16  * software, please contact iptel.org by e-mail at the following addresses:
17  *    info@iptel.org
18  *
19  * ser is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License 
25  * along with this program; if not, write to the Free Software 
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27  */
28
29
30 #include "../../dprint.h"
31 #include "../../config.h"
32 #include "../../parser/parser_f.h"
33 #include "../../ut.h"
34 #include "../../timer.h"
35 #include "../../hash_func.h"
36 #include "../../globals.h"
37 #include "../../dset.h"
38 #include "t_funcs.h"
39 #include "t_hooks.h"
40 #include "t_msgbuilder.h"
41 #include "ut.h"
42 #include "t_cancel.h"
43 #include "t_lookup.h"
44 #include "t_fwd.h"
45 #include "fix_lumps.h"
46 #include "config.h"
47
48
49 #ifdef _OBSOLETED
50 #define shm_free_lump( _lmp) \
51         do{\
52                 if ((_lmp)) {\
53                         if ((_lmp)->op==LUMP_ADD && (_lmp)->u.value )\
54                                 shm_free((_lmp)->u.value);\
55                         shm_free((_lmp));\
56                 }\
57         }while(0);
58 #endif
59
60 char *print_uac_request( struct cell *t, struct sip_msg *i_req,
61         int branch, str *uri, unsigned int *len, struct socket_info *send_sock )
62 {
63         char *buf, *shbuf;
64
65         shbuf=0;
66
67         /* ... we calculate branch ... */       
68         if (!t_calc_branch(t, branch, i_req->add_to_branch_s,
69                         &i_req->add_to_branch_len ))
70         {
71                 LOG(L_ERR, "ERROR: print_uac_request: branch computation failed\n");
72                 goto error01;
73         }
74
75         /* ... update uri ... */
76         i_req->new_uri=*uri;
77
78         /* ... give apps a chance to change things ... */
79         callback_event( TMCB_REQUEST_OUT, t, i_req, -i_req->REQ_METHOD);
80
81         /* ... and build it now */
82         buf=build_req_buf_from_sip_req( i_req, len, send_sock, i_req->rcv.proto );
83 #ifdef DBG_MSG_QA
84         if (buf[*len-1]==0) {
85                 LOG(L_ERR, "ERROR: print_uac_request: sanity check failed\n");
86                 abort();
87         }
88 #endif
89         if (!buf) {
90                 LOG(L_ERR, "ERROR: print_uac_request: no pkg_mem\n"); 
91                 ser_error=E_OUT_OF_MEM;
92                 goto error01;
93         }
94         /*      clean Via's we created now -- they would accumulate for
95                 other branches  and for  shmem i_req they would mix up
96                 shmem with pkg_mem
97         */
98 #ifdef OBSOLETED
99         if (branch) for(b=i_req->add_rm,b1=0;b;b1=b,b=b->next)
100                 if (b->type==HDR_VIA) {
101                         for(a=b->before;a;)
102                                 {c=a->before;free_lump(a);pkg_free(a);a=c;}
103                         for(a=b->after;a;)
104                                 {c=a->after;free_lump(a);pkg_free(a);a=c;}
105                         if (b1) b1->next = b->next;
106                         else i_req->add_rm = b->next;
107                         free_lump(b);pkg_free(b);
108                 }
109 #endif
110         free_via_lump(&i_req->add_rm);
111
112         shbuf=(char *)shm_malloc(*len);
113         if (!shbuf) {
114                 ser_error=E_OUT_OF_MEM;
115                 LOG(L_ERR, "ERROR: print_uac_request: no shmem\n");
116                 goto error02;
117         }
118         memcpy( shbuf, buf, *len );
119
120 error02:
121         pkg_free( buf );
122 error01:
123         return shbuf;
124 }
125
126 /* introduce a new uac to transaction; returns its branch id (>=0)
127    or error (<0); it doesn't send a message yet -- a reply to it
128    might itnerfere with the processes of adding multiple branches
129 */
130 int add_uac( struct cell *t, struct sip_msg *request, str *uri, 
131         struct proxy_l *proxy )
132 {
133
134         int ret;
135         short temp_proxy;
136         union sockaddr_union to;
137         unsigned short branch;
138         struct socket_info* send_sock;
139         char *shbuf;
140         unsigned int len;
141
142         branch=t->nr_of_outgoings;
143         if (branch==MAX_BRANCHES) {
144                 LOG(L_ERR, "ERROR: add_uac: maximum number of branches exceeded\n");
145                 ret=E_CFG;
146                 goto error;
147         }
148
149         /* check existing buffer -- rewriting should never occur */
150         if (t->uac[branch].request.buffer) {
151                 LOG(L_CRIT, "ERROR: add_uac: buffer rewrite attempt\n");
152                 ret=ser_error=E_BUG;
153                 goto error;
154         }
155
156         /* check DNS resolution */
157         if (proxy) temp_proxy=0; else {
158                 proxy=uri2proxy( uri );
159                 if (proxy==0)  {
160                         ret=E_BAD_ADDRESS;
161                         goto error;
162                 }
163                 temp_proxy=1;
164         }
165
166         if (proxy->ok==0) {
167                 if (proxy->host.h_addr_list[proxy->addr_idx+1])
168                         proxy->addr_idx++;
169                 else proxy->addr_idx=0;
170                 proxy->ok=1;
171         }
172
173         hostent2su( &to, &proxy->host, proxy->addr_idx, 
174                 proxy->port ? htons(proxy->port):htons(SIP_PORT));
175
176         send_sock=get_send_socket( &to , request->rcv.proto);
177         if (send_sock==0) {
178                 LOG(L_ERR, "ERROR: add_uac: can't fwd to af %d "
179                         " (no corresponding listening socket)\n",
180                         to.s.sa_family );
181                 ret=ser_error=E_NO_SOCKET;
182                 goto error01;
183         }
184
185         /* now message printing starts ... */
186         shbuf=print_uac_request( t, request, branch, uri, 
187                 &len, send_sock );
188         if (!shbuf) {
189                 ret=ser_error=E_OUT_OF_MEM;
190                 goto error01;
191         }
192
193         /* things went well, move ahead and install new buffer! */
194         t->uac[branch].request.to=to;
195         t->uac[branch].request.send_sock=send_sock;
196         t->uac[branch].request.buffer=shbuf;
197         t->uac[branch].request.buffer_len=len;
198         t->uac[branch].uri.s=t->uac[branch].request.buffer+
199                 request->first_line.u.request.method.len+1;
200         t->uac[branch].uri.len=uri->len;
201         t->nr_of_outgoings++;
202
203         /* update stats */
204         proxy->tx++;
205         proxy->tx_bytes+=len;
206
207         /* done! */     
208         ret=branch;
209                 
210 error01:
211         if (temp_proxy) {
212                 free_proxy( proxy );
213                 free( proxy );
214         }
215 error:
216         return ret;
217 }
218
219 int e2e_cancel_branch( struct sip_msg *cancel_msg, struct cell *t_cancel, 
220         struct cell *t_invite, int branch )
221 {
222         int ret;
223         char *shbuf;
224         unsigned int len;
225
226         if (t_cancel->uac[branch].request.buffer) {
227                 LOG(L_CRIT, "ERROR: e2e_cancel_branch: buffer rewrite attempt\n");
228                 ret=ser_error=E_BUG;
229                 goto error;
230         }       
231
232         /* note -- there is a gap in proxy stats -- we don't update 
233            proxy stats with CANCEL (proxy->ok, proxy->tx, etc.)
234         */
235
236         /* print */
237         shbuf=print_uac_request( t_cancel, cancel_msg, branch, 
238                 &t_invite->uac[branch].uri, &len, 
239                 t_invite->uac[branch].request.send_sock);
240         if (!shbuf) {
241                 LOG(L_ERR, "ERROR: e2e_cancel_branch: printing e2e cancel failed\n");
242                 ret=ser_error=E_OUT_OF_MEM;
243                 goto error;
244         }
245         
246         /* install buffer */
247         t_cancel->uac[branch].request.to=t_invite->uac[branch].request.to;
248         t_cancel->uac[branch].request.send_sock=t_invite->uac[branch].request.send_sock;
249         t_cancel->uac[branch].request.buffer=shbuf;
250         t_cancel->uac[branch].request.buffer_len=len;
251         t_cancel->uac[branch].uri.s=t_cancel->uac[branch].request.buffer+
252                 cancel_msg->first_line.u.request.method.len+1;
253         t_cancel->uac[branch].uri.len=t_invite->uac[branch].uri.len;
254         
255
256         /* success */
257         ret=1;
258
259
260 error:
261         return ret;
262 }
263
264 void e2e_cancel( struct sip_msg *cancel_msg, 
265         struct cell *t_cancel, struct cell *t_invite )
266 {
267         branch_bm_t cancel_bm;
268         int i;
269         int lowest_error;
270         str backup_uri;
271         int ret;
272
273         cancel_bm=0;
274         lowest_error=0;
275
276         backup_uri=cancel_msg->new_uri;
277         /* determine which branches to cancel ... */
278         which_cancel( t_invite, &cancel_bm );
279         t_cancel->nr_of_outgoings=t_invite->nr_of_outgoings;
280         /* fix label -- it must be same for reply matching */
281         t_cancel->label=t_invite->label;
282         /* ... and install CANCEL UACs */
283         for (i=0; i<t_invite->nr_of_outgoings; i++)
284                 if (cancel_bm & (1<<i)) {
285                         ret=e2e_cancel_branch(cancel_msg, t_cancel, t_invite, i);
286                         if (ret<0) cancel_bm &= ~(1<<i);
287                         if (ret<lowest_error) lowest_error=ret;
288                 }
289         cancel_msg->new_uri=backup_uri;
290
291         /* send them out */
292         for (i=0; i<t_cancel->nr_of_outgoings; i++) {
293                 if (cancel_bm & (1<<i)) {
294                         if (SEND_BUFFER( &t_cancel->uac[i].request)==-1) {
295                                 LOG(L_ERR, "ERROR: e2e_cancel: send failed\n");
296                         }
297                         start_retr( &t_cancel->uac[i].request );
298                 }
299         }
300
301
302         /* if error occured, let it know upstream (final reply
303            will also move the transaction on wait state
304         */
305         if (lowest_error<0) {
306                 LOG(L_ERR, "ERROR: cancel error\n");
307                 t_reply( t_cancel, cancel_msg, 500, "cancel error");
308         /* if there are pending branches, let upstream know we
309            are working on it
310         */
311         } else if (cancel_bm) {
312                 DBG("DEBUG: e2e_cancel: e2e cancel proceeding\n");
313                 t_reply( t_cancel, cancel_msg, 200, CANCELLING );
314         /* if the transaction exists, but there is no more pending
315            branch, tell usptream we're done
316         */
317         } else {
318                 DBG("DEBUG: e2e_cancel: e2e cancel -- no more pending branches\n");
319                 t_reply( t_cancel, cancel_msg, 200, CANCEL_DONE );
320         }
321         /* we could await downstream UAS's 487 replies; however,
322            if some of the branches does not do that, we could wait
323            long time and annoy upstream UAC which wants to see 
324            a result of CANCEL quickly
325         */
326         DBG("DEBUG: e2e_cancel: sending 487\n");
327         /* in case that something in the meantime has been sent upstream
328            (like if FR hit at the same time), don't try to send */
329         if (t_invite->uas.status>=200) return;
330         /* there is still a race-condition -- the FR can hit now; that's
331            not too bad -- we take care in t_reply's REPLY_LOCK; in
332            the worst case, both this t_reply and other replier will
333            try, and the later one will result in error message 
334            "can't reply twice"
335         */
336         t_reply(t_invite, t_invite->uas.request, 487, CANCELLED );
337 }
338
339
340 /* function returns:
341  *       1 - forward successfull
342  *      -1 - error during forward
343  */
344 int t_forward_nonack( struct cell *t, struct sip_msg* p_msg , 
345         struct proxy_l * proxy )
346 {
347         str          backup_uri;
348         int branch_ret, lowest_ret;
349         str current_uri;
350         branch_bm_t     added_branches;
351         int first_branch;
352         int i;
353         struct cell *t_invite;
354
355         /* make -Wall happy */
356         current_uri.s=0;
357
358         t->kr|=REQ_FWDED;
359
360         if (p_msg->REQ_METHOD==METHOD_CANCEL) {
361                 t_invite=t_lookupOriginalT(  p_msg );
362                 if (t_invite!=T_NULL) {
363                         e2e_cancel( p_msg, t, t_invite );
364                         UNREF(t_invite);
365                         return 1;
366                 }
367         }
368
369         /* backup current uri ... add_uac changes it */
370         backup_uri = p_msg->new_uri;
371         /* if no more specific error code is known, use this */
372         lowest_ret=E_BUG;
373         /* branches added */
374         added_branches=0;
375         /* branch to begin with */
376         first_branch=t->nr_of_outgoings;
377
378         /* on first-time forwarding, use current uri, later only what
379            is in additional branches (which may be continuously refilled
380         */
381         if (first_branch==0) {
382                 branch_ret=add_uac( t, p_msg, 
383                         p_msg->new_uri.s ? &p_msg->new_uri :  
384                                 &p_msg->first_line.u.request.uri,
385                         proxy );
386                 if (branch_ret>=0) 
387                         added_branches |= 1<<branch_ret;
388                 else
389                         lowest_ret=branch_ret;
390         }
391
392         init_branch_iterator();
393         while((current_uri.s=next_branch( &current_uri.len))) {
394                 branch_ret=add_uac( t, p_msg, &current_uri, proxy );
395                 /* pick some of the errors in case things go wrong;
396                    note that picking lowest error is just as good as
397                    any other algorithm which picks any other negative
398                    branch result */
399                 if (branch_ret>=0) 
400                         added_branches |= 1<<branch_ret;
401                 else
402                         lowest_ret=branch_ret;
403         }
404         /* consume processed branches */
405         clear_branches();
406
407         /* restore original URI */
408         p_msg->new_uri=backup_uri;
409
410         /* don't forget to clear all branches processed so far */
411
412         /* things went wrong ... no new branch has been fwd-ed at all */
413         if (added_branches==0)
414                 return lowest_ret;
415
416         /* if someone set on_negative, store in in T-context */
417         t->on_negative=get_on_negative();
418
419         /* send them out now */
420         for (i=first_branch; i<t->nr_of_outgoings; i++) {
421                 if (added_branches & (1<<i)) {
422                         if (SEND_BUFFER( &t->uac[i].request)==-1) {
423                                 LOG(L_ERR, "ERROR: add_uac: sending request failed\n");
424                                 if (proxy) { proxy->errors++; proxy->ok=0; }
425                         }
426                         start_retr( &t->uac[i].request );
427                 }
428         }
429         return 1;
430 }       
431
432 int t_replicate(struct sip_msg *p_msg,  struct proxy_l *proxy )
433 {
434         /* this is a quite horrible hack -- we just take the message
435            as is, including Route-s, Record-route-s, and Vias ,
436            forward it downstream and prevent replies received
437            from relaying by setting the replication/local_trans bit;
438
439                 nevertheless, it should be good enough for the primary
440                 customer of this function, REGISTER replication
441
442                 if we want later to make it thoroughly, we need to
443                 introduce delete lumps for all the header fields above
444         */
445         return t_relay_to(p_msg, proxy, 1 /* replicate */);
446 }