ser_error processing, ipv6-ization of TM, new TM callbacks;
[sip-router] / modules / tm / t_reply.c
1 /*
2  * $Id$
3  *
4  */
5
6
7 #include "hash_func.h"
8 #include "t_funcs.h"
9 #include "../../dprint.h"
10 #include "../../config.h"
11 #include "../../parser/parser_f.h"
12 #include "../../ut.h"
13 #include "../../timer.h"
14
15 #include "t_hooks.h"
16
17
18
19
20 inline int check_for_no_response( struct cell *Trans ,int code, int relay)
21 {
22         if ( code/100>=3 && Trans->uac[Trans->nr_of_outgoings].uri.s )
23         {
24                 forward_serial_branch( Trans , Trans->nr_of_outgoings );
25                 return -1;
26         }
27         return relay;
28 }
29
30
31
32 /* Retransmits the last sent inbound reply.
33  * input: p_msg==request for which I want to retransmit an associated reply
34  * Returns  -1 - error
35  *           1 - OK
36  */
37 int t_retransmit_reply( /* struct sip_msg* p_msg    */ )
38 {
39         static char b[BUF_SIZE];
40         int len;
41
42         if (!T->uas.response.buffer)
43                 return -1;
44
45         if ( (len=T->uas.response.buffer_len)==0 || len>BUF_SIZE ) {
46                 UNLOCK_REPLIES( T );
47                 return -2;
48         }
49         memcpy( b, T->uas.response.buffer, len );
50         UNLOCK_REPLIES( T );
51         SEND_PR_BUFFER( & T->uas.response, b, len );
52         return 1;
53 }
54
55
56
57
58
59 /* Force a new response into inbound response buffer.
60   * returns 1 if everything was OK or -1 for error
61   */
62 int t_send_reply( struct sip_msg* p_msg, unsigned int code, 
63         char * text, unsigned int branch)
64 {
65         unsigned int len, buf_len=0;
66         char * buf;
67         struct retr_buf *rb;
68         int relay, save_clone;
69         struct socket_info* send_sock;
70
71         buf = build_res_buf_from_sip_req(code,text,T->uas.tag->s,
72                 T->uas.tag->len, T->uas.request,&len);
73         DBG("DEBUG: t_send_reply: buffer computed\n");
74         if (!buf)
75         {
76                 DBG("DEBUG: t_send_reply: response building failed\n");
77                 goto error;
78         }
79
80         LOCK_REPLIES( T );
81         relay = t_should_relay_response(T, code, branch, &save_clone);
82
83         if (save_clone)
84         {
85                 T->uac[branch].status = code;
86         }
87
88         rb = & T->uas.response;
89         if (relay >=0 && (relay=check_for_no_response(T,code,relay))>=0 )
90         {
91                 if (!rb->buffer) {
92                         /* initialize retransmission structure */
93                         if (update_sock_struct_from_via(  &(rb->to),  p_msg->via1 )==-1)
94                         {
95                                 UNLOCK_REPLIES( T );
96                                 LOG(L_ERR,"ERROR: t_send_reply: cannot lookup reply dst: %s\n",
97                                         p_msg->via1->host.s );
98                                 goto error2;
99                         }
100                         send_sock=get_send_socket(&rb->to);
101                         if (send_sock==0) {
102                                 LOG(L_ERR, "ERROR: t_send_reply: cannot fwd to af %d "
103                                         "no socket\n", rb->to.s.sa_family);
104                                 ser_error=E_NO_SOCKET;
105                                 goto error2;
106                         }
107                         rb->send_sock=send_sock;
108                         /* rb->to.sin_family = AF_INET; */
109                         rb->activ_type = code;
110                         buf_len = len + REPLY_OVERBUFFER_LEN;
111                 }else{
112                         buf_len = len;
113                 }
114                 /* puts the reply's buffer to uas.response */
115                 if (! (rb->buffer = (char*)shm_resize( rb->buffer, buf_len )))
116                 {
117                         UNLOCK_REPLIES( T );
118                         LOG(L_ERR, "ERROR: t_send_reply: cannot allocate shmem buffer\n");
119                         goto error2;
120                 }
121                 rb->buffer_len = len ;
122                 memcpy( rb->buffer , buf , len );
123                 T->uas.status = code;
124                 /* needs to be protected too because what timers are set depends
125                    on current transactions status */
126                 t_update_timers_after_sending_reply( rb );
127         } /* if realy */
128
129         UNLOCK_REPLIES( T );
130
131         if (relay>=0) SEND_PR_BUFFER( rb, buf, len );
132         pkg_free( buf ) ;
133         DBG("DEBUG: t_send_reply: finished\n");
134         return 1;
135
136 error2:
137         pkg_free ( buf );
138 error:
139         return -1;
140 }
141
142
143
144 #if 0
145 /* Push a previously stored reply from UA Client to UA Server
146  * and send it out */
147 static int push_reply( struct cell* trans , unsigned int branch ,
148                                                                                                 char *buf, unsigned int len)
149 {
150         unsigned int buf_len;
151         struct retrans_buff *rb;
152
153         DBG("DEBUG: push_reply_from_uac_to_uas: start\n");
154         rb= & trans->outbound_response;
155         /* if there is a reply, release the buffer (everything else stays same) */
156         if ( ! rb->retr_buffer ) {
157                 /*init retrans buffer*/
158                 memset( rb , 0 , sizeof (struct retrans_buff) );
159                 if (update_sock_struct_from_via(  &(rb->to),
160                         trans->inbound_response[branch]->via2 )==-1) {
161                                 LOG(L_ERR, "ERROR: push_reply_from_uac_to_uas: "
162                                         "cannot lookup reply dst: %s\n",
163                                 trans->inbound_response[branch]->via2->host.s );
164                                 goto error;
165                 }
166                 rb->retr_timer.tg=TG_RT;
167                 rb->fr_timer.tg=TG_FR;
168                 rb->retr_timer.payload = rb;
169                 rb->fr_timer.payload =  rb;
170                 rb->to.sin_family = AF_INET;
171                 rb->my_T = trans;
172                 rb->status = trans->inbound_response[branch]->REPLY_STATUS;
173         };
174
175         /* if this is a first reply (?100), longer replies will probably follow;
176         try avoiding shm_resize by higher buffer size */
177         buf_len = rb->retr_buffer ? len : len + REPLY_OVERBUFFER_LEN;
178         if (! (rb->retr_buffer = (char*)shm_resize( rb->retr_buffer, buf_len )))
179         {
180                 LOG(L_ERR, "ERROR: t_push: cannot allocate shmem buffer\n");
181                 goto error1;
182         }
183         rb->bufflen = len ;
184         memcpy( rb->retr_buffer , buf , len );
185
186         /* update the status*/
187         trans->status = trans->inbound_response[branch]->REPLY_STATUS;
188         if ( trans->inbound_response[branch]->REPLY_STATUS>=200 &&
189                 trans->relaied_reply_branch==-1 ) {
190
191                 memcpy( & trans->ack_to, & trans->outbound_request[ branch ]->to,
192                         sizeof( struct sockaddr_in ) );
193                 trans->relaied_reply_branch = branch;
194         }
195
196         /*send the reply*/
197         SEND_BUFFER( rb );
198         return 1;
199
200 error1:
201 error:
202         return -1;
203 }
204 #endif
205
206
207 /*  This function is called whenever a reply for our module is received; 
208   * we need to register  this function on module initialization;
209   *  Returns :   0 - core router stops
210   *              1 - core router relay statelessly
211   */
212 int t_on_reply( struct sip_msg  *p_msg )
213 {
214         int branch, msg_status, msg_class, save_clone;
215         int local_cancel;
216         int relay;
217         int start_fr = 0;
218         int is_invite;
219         /* retransmission structure of outbound reply and request */
220         struct retr_buf *rb=0;
221         char *buf=0, *ack=0;
222         /* length of outbound reply */
223         unsigned int res_len, ack_len;
224         /* buffer length (might be somewhat larger than message size */
225         unsigned int alloc_len;
226         str *str_foo;
227         struct socket_info* send_sock;
228
229
230         /* make sure we know the assosociated tranaction ... */
231         if (t_check( p_msg  , &branch , &local_cancel)==-1)
232                 return 1;
233         /* ... if there is no such, tell the core router to forward statelessly */
234         if ( T<=0 ) return 1;
235
236         DBG("DEBUG: t_on_reply: org. status uas=%d, uac[%d]=%d loca_cancel=%d)\n",
237                 T->uas.status, branch, T->uac[branch].status, local_cancel);
238
239         /* special cases (local cancel reply) -bogdan */
240         if (local_cancel==1)
241         {
242                 reset_timer( hash_table, &(T->uac[branch].request.retr_timer));
243                 if ( p_msg->REPLY_STATUS>=200 )
244                         reset_timer(hash_table,&(T->uac[branch].request.fr_timer));
245                 goto error;
246         }
247
248         /* do we have via2 ? - maybe we'll need it for forwarding -bogdan*/
249         if ((p_msg->via2==0) || (p_msg->via2->error!=VIA_PARSE_OK)){
250                 /* no second via => error */
251                 LOG(L_ERR, "ERROR: t_on_reply: no 2nd via found in reply\n");
252                 goto error;
253         }
254
255         msg_status=p_msg->REPLY_STATUS;
256         msg_class=REPLY_CLASS(p_msg);
257         is_invite= T->uas.request->REQ_METHOD==METHOD_INVITE;
258
259 #ifdef _DONT_DO_IT_MAN
260 /*  generate the retrans buffer, make a simplified
261         assumption everything but 100 will be fwd-ed;
262         sometimes it will result in useless CPU cycles
263         but mostly the assumption holds and allows the
264         work to be done out of criticial lock region */
265         if (msg_status==100 && T->uac[branch].status)
266                 buf=0;
267         else {
268                 /* buf maybe allo'ed*/
269                 buf = build_res_buf_from_sip_res ( p_msg, &res_len);
270                 if (!buf) {
271                         LOG(L_ERR, "ERROR: t_on_reply_received: "
272                         "no mem for outbound reply buffer\n");
273                         goto error;
274                 }
275         }
276 #endif
277
278         /* *** stop timers *** */
279         /* stop retransmission */
280         reset_timer( hash_table, &(T->uac[branch].request.retr_timer));
281         /* stop final response timer only if I got a final response */
282         if ( msg_class>1 )
283                 reset_timer( hash_table, &(T->uac[branch].request.fr_timer));
284
285         LOCK_REPLIES( T );
286         /* if a got the first prov. response for an INVITE ->
287            change FR_TIME_OUT to INV_FR_TIME_UT */
288         start_fr = !T->uac[branch].rpl_received && msg_class==1 && is_invite;
289
290         /* *** store and relay message as needed *** */
291         relay = t_should_relay_response( T , msg_status, branch, &save_clone );
292         DBG("DEBUG: t_on_reply: branch=%d, save=%d, relay=%d\n",
293                 branch, save_clone, relay );
294
295         if (save_clone)
296         {
297                 str_foo = &(T->uac[branch].tag);
298                 str_foo->s = shm_resize(str_foo->s, (str_foo?0:TAG_OVERBUFFER_LEN) +
299                         get_to(p_msg)->tag_value.len);
300                 if (!str_foo->s)
301                 {
302                         LOG( L_ERR , "ERROR: t_on_reply: connot alocate memory!\n");
303                         goto error1;
304                 }
305                 /* when forking, replies greater then 300 are saved */
306                 if ((T->nr_of_outgoings>1 || T->uac[T->nr_of_outgoings].uri.s)
307                         && msg_status>=300 )
308                 {
309                         DBG("DEBUG: t_on_reply: saving reply! \n");
310                         str_foo = &(T->uac[branch].rpl_buffer);
311                         str_foo->s = shm_resize(str_foo->s, res_len+
312                                 (str_foo->s?0:REPLY_OVERBUFFER_LEN) );
313                         if (!str_foo->s)
314                         {
315                                 LOG( L_ERR , "ERROR: t_on_reply: connot alocate memory!\n");
316                                 goto error1;
317                         }
318                         memcpy(str_foo->s,buf,res_len);
319                         str_foo->len = res_len;
320                 }
321                 /*copy the TO tag from reply*/
322                 T->uac[branch].tag.len = get_to(p_msg)->tag_value.len;
323                 memcpy( T->uac[branch].tag.s, get_to(p_msg)->tag_value.s,
324                         T->uac[branch].tag.len );
325                 T->uac[branch].rpl_received = 1;
326                 T->uac[branch].status = msg_status;
327         }
328
329         rb = & T->uas.response;
330         if (relay >= 0  && (relay=check_for_no_response(T,msg_status,relay))>=0 ) {
331
332                 buf = build_res_buf_from_sip_res ( p_msg, &res_len);
333                 if (!buf) {
334                         UNLOCK_REPLIES( T );
335                         start_fr = 1;
336                         LOG(L_ERR, "ERROR: t_on_reply_received: "
337                                 "no mem for outbound reply buffer\n");
338                         goto error1;
339                 }
340                 callback_event( TMCB_REPLY_IN, p_msg );
341
342                 if (relay!=branch)
343                 {
344                         str_foo = &(T->uac[relay].rpl_buffer);
345                         if (buf) pkg_free(buf);
346                         buf = (char*)pkg_malloc(str_foo->len);
347                         if (!buf)
348                         {
349                                 UNLOCK_REPLIES( T );
350                                 start_fr = 1;
351                                 LOG(L_ERR, "ERROR: t_on_reply: cannot alloc pkg mem\n");
352                                 goto error1;
353                         }
354                         memcpy( buf , str_foo->s , str_foo->len );
355                         res_len = str_foo->len;
356                 }
357                 /* if there is no reply yet, initialize the structure */
358                 if ( ! rb->buffer ) {
359                         /*init retrans buffer*/
360                         if (update_sock_struct_from_via( &(rb->to),p_msg->via2 )==-1) {
361                                 UNLOCK_REPLIES( T );
362                                 start_fr = 1;
363                                 LOG(L_ERR, "ERROR: t_on_reply: cannot lookup reply dst: %s\n",
364                                         p_msg->via2->host.s );
365                                 goto error1;
366                         }
367                         send_sock=get_send_socket(&rb->to);
368                         if (send_sock==0) {
369                                 UNLOCK_REPLIES( T );
370                                 LOG(L_ERR, "ERROR: t_on_reply: cannot fwd to af %d "
371                                         "no socket\n", rb->to.s.sa_family);
372                                 start_fr=1;
373                                 goto error1;
374                         }
375                         /* rb->to.sin_family = AF_INET; */
376                         rb->send_sock=send_sock;
377                         rb->activ_type = p_msg->REPLY_STATUS;
378                         /* allocate something more for the first message;
379                            subsequent messages will be longer and buffer
380                            reusing will save us a malloc lock */
381                         alloc_len = res_len + REPLY_OVERBUFFER_LEN ;
382                 }else{
383                         alloc_len = res_len;
384                 }
385                 /* puts the reply's buffer to uas.response */
386                 if (! (rb->buffer = (char*)shm_resize( rb->buffer, alloc_len ))) {
387                         UNLOCK_REPLIES( T );
388                         start_fr = 1;
389                         LOG(L_ERR, "ERROR: t_on_reply: cannot alloc shmem\n");
390                         goto error1;
391                 }
392                 rb->buffer_len = res_len;
393                 memcpy( rb->buffer, buf, res_len );
394                 /* update the status ... */
395                 T->uas.status = p_msg->REPLY_STATUS;
396                 T->uas.tag=&(T->uac[relay].tag);
397                 if (T->uas.status >=200 && T->relaied_reply_branch==-1 )
398                                 T->relaied_reply_branch = relay;
399         }; /* if relay ... */
400
401         UNLOCK_REPLIES( T );
402
403         if (relay >= 0) {
404                 SEND_PR_BUFFER( rb, buf, res_len );
405                 t_update_timers_after_sending_reply( rb );
406                 callback_event( TMCB_REPLY, p_msg );
407         }
408
409         /* *** ACK handling *** */
410         if ( is_invite ) {
411                 if ( T->uac[branch].request.ack_len )
412                 {   /*retransmit*/
413                         /* I don't need any additional syncing here -- after ack
414                            is introduced it's never changed */
415                         DBG("DEBUG: localy cached ACK retranmitted\n");
416                         SEND_ACK_BUFFER( &(T->uac[branch].request) );
417                 } else if (msg_class>2 ) {
418                         /*on a non-200 reply to INVITE*/
419                         DBG("DEBUG: t_on_reply_received: >=3xx reply to INVITE:"
420                                 "send ACK\n");
421                         ack = build_ack( p_msg, T, branch , &ack_len);
422                         if (ack) {
423                                 SEND_PR_BUFFER( &(T->uac[branch].request), ack, ack_len );
424                                 /* append to transaction structure */
425                                 attach_ack( T, branch, ack , ack_len );
426                         } else {
427                                 /* restart FR */
428                                 start_fr=1;
429                                 DBG("ERROR: t_on_reply: build_ack failed\n");
430                         }
431                 }
432         } /* is_invite */
433
434         /* restart retransmission if a provisional response came for
435            a non_INVITE -> retrasmit at RT_T2*/
436         if ( msg_class==1 && !is_invite )
437         {
438                 rb->retr_list = RT_T2;
439                 set_timer( hash_table, &(rb->retr_timer), RT_T2 );
440         }
441 error1:
442         if (start_fr)
443                 set_timer( hash_table, &(rb->fr_timer), FR_INV_TIMER_LIST );
444         if (buf) pkg_free( buf );
445 error:
446         T_UNREF( T );
447         /* don't try to relay statelessly on error; on troubles, simply do nothing;
448            that will make the other party to retransmit; hopefuly, we'll then 
449            be better off */
450         return 0;
451 }
452
453
454 /* Checks if the new reply (with new_code status) should be sent or not
455  *  based on the current
456  * transactin status.
457  * Returns      - branch number (0,1,...) which should be relayed
458  *         -1 if nothing to be relayed
459  */
460 int t_should_relay_response( struct cell *Trans , int new_code,
461                                                                         int branch , int *should_store )
462 {
463         //int T_code;
464         int b, lowest_b, lowest_s;
465
466         //if (Trans->uas.request->REQ_METHOD==METHOD_INVITE)
467         //      T_code = Trans->uac[branch].status;
468         //else
469         //T_code = Trans->uas.status;
470
471         /* note: this code never lets replies to CANCEL go through;
472            we generate always a local 200 for CANCEL; 200s are
473            not relayed because it's not an INVITE transaction;
474            >= 300 are not relayed because 200 was already sent
475            out
476         */
477         DBG("->>>>>>>>> T_code=%d, new_code=%d\n",Trans->uas.status,new_code);
478         /* if final response sent out, allow only INVITE 2xx  */
479         if ( Trans->uas.status >= 200 ) {
480                 if (new_code>=200 && new_code < 300  && 
481                         Trans->uas.request->REQ_METHOD==METHOD_INVITE) {
482                         DBG("DBG: t_should_relay: 200 INV after final sent\n");
483                         *should_store=1;
484                         return branch;
485                 } else {
486                         *should_store=0;
487                         return -1;
488                 }
489         } else { /* no final response sent yet */
490                 /* negative replies subject to fork picking */
491                 if (new_code >=300 ) {
492                         /* dirty hack by Jiri -- subject to clean up as all the
493                            reply_processing crap; if there are no branches at
494                            all, I guess TM wants to reply itself and allow that
495                         */
496                         if (Trans->nr_of_outgoings==0)
497                                 return 0;
498                         *should_store=1;
499                         /* if all_final return lowest */
500                         lowest_b=-1; lowest_s=999;
501                         for ( b=0; b<Trans->nr_of_outgoings ; b++ ) {
502                                 /* "fake" for the currently processed branch */
503                                 if (b==branch) {
504                                         if (new_code<lowest_s) {
505                                                 lowest_b=b;
506                                                 lowest_s=new_code;
507                                         }
508                                         continue;
509                                 }
510                                 /* there is still an unfinished UAC transaction; wait now! */
511                                 if ( Trans->uac[b].status<200 )
512                                         return -1;
513                                 if ( Trans->uac[b].status<lowest_s )
514                                 {
515                                         lowest_b =b;
516                                         lowest_s = T->uac[b].status;
517                                 }
518                         }
519                         return lowest_b;
520                 /* 1xx except 100 and 2xx will be relayed */
521                 } else if (new_code>100) {
522                         *should_store=1;
523                         return branch;
524                 }
525                 /* 100 won't be relayed */
526                 else {
527                         if (!T->uac[branch].rpl_received) *should_store=1;
528                                 else *should_store=0;
529                         if (Trans->uas.status==0) return branch;
530                                 else return -1;
531                 }
532         }
533
534         LOG(L_CRIT, "ERROR: Oh my gooosh! We don't know whether to relay\n");
535         abort();
536 }
537